我有这个代码。我想遍历数组并为每个条目创建一个新文档。如果我手动将循环长度设置为它工作正常的行数。我想将它设置为循环数组的长度。但是,.length 属性始终返回 null。我错过了什么。我也尝试过每个循环但没有运气。 function createDocument() { //var headers = Sheets.Spreadsheets.Values.get('fileID', 'A1:Z1'); var studentHistory = Sheets.Spreadsheets.Values.get('fileID', 'A2:Z200'); var templateId = 'fileID'; var documentId; var dstFolder = DriveApp.getFolderById('folderID'); var length = studentHistory.length; Logger.log(studentHistory); Logger.log(length); //Loop through rows in sheet for (var i = 0; i < length; i++){ //Get values from sheet row var date = studentHistory.values[i][0]; var studentName = studentHistory.values[i][1]; var dob = studentHistory.values[i][2]; var pcDoctor = studentHistory.values[i][3]; var address = studentHistory.values[i][4]; var fgName = studentHistory.values[i][5]; var mgName = studentHistory.values[i][6]; var phoneMom = studentHistory.values[i][7]; var phoneDad = studentHistory.values[i][8]; var empMom = studentHistory.values[i][9]; var empDad = studentHistory.values[i][10]; var livesWith = studentHistory.values[i][11]; var childrenInHome = studentHistory.values[i][12]; var childrenNotInHome = studentHistory.values[i][13]; var othersInHome = studentHistory.values[i][14]; var illnesses = studentHistory.values[i][15]; var illnessDetails = studentHistory.values[i][16]; var hospitalizations = studentHistory.values[i][17]; var hospDetails = studentHistory.values[i][18]; var trauma = studentHistory.values[i][19]; var traumaDetails = studentHistory.values[i][20]; var injuries = studentHistory.values[i][21]; var injuryDetails = studentHistory.values[i][22]; var medications = studentHistory.values[i][23]; var additionalComments = studentHistory.values[i][24]; var otherSchools = studentHistory.values[i][25]; }}
2 回答
明月笑刀无情
TA贡献1828条经验 获得超4个赞
studentHistory.values
是数组。
因此,试试这个来获取长度:
var length = studentHistory.values.length;
呼如林
TA贡献1798条经验 获得超3个赞
解决方案
我看到您正在使用 Advanced Google Services 调用 Sheets API。此 Apps 脚本类允许您直接从自动处理授权过程的脚本中调用 Google API。
但是,它不能作为内置类使用,例如在SpreadsheetApp包装器中可用。
您的请求将按照以下规范返回类似 HTTP 的响应:
{
"range": string,
"majorDimension": enum (Dimension),
"values": [
array
]
}
您将需要解析这些响应以获得所需的结果。
建议修改
function createDocument()
{
//var headers = Sheets.Spreadsheets.Values.get('fileID', 'A1:Z1');
var studentHistory = Sheets.Spreadsheets.Values.get('fileID', 'A2:Z200');
var templateId = 'fileID';
var documentId;
var dstFolder = DriveApp.getFolderById('folderID');
var length = studentHistory.values.length;
...
添加回答
举报
0/150
提交
取消