.length 不适用于 Google Apps 脚本中的数组

我有这个代码。我想遍历数组并为每个条目创建一个新文档。如果我手动将循环长度设置为它工作正常的行数。我想将它设置为循环数组的长度。但是,.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];

  }

}


扬帆大鱼
浏览 96回答 2
2回答

明月笑刀无情

studentHistory.values是数组。因此,试试这个来获取长度:var&nbsp;length&nbsp;=&nbsp;studentHistory.values.length;

呼如林

解决方案我看到您正在使用 Advanced Google Services 调用 Sheets API。此 Apps 脚本类允许您直接从自动处理授权过程的脚本中调用 Google API。但是,它不能作为内置类使用,例如在SpreadsheetApp包装器中可用。您的请求将按照以下规范返回类似 HTTP 的响应:{&nbsp; "range": string,&nbsp; "majorDimension": enum (Dimension),&nbsp; "values": [&nbsp; &nbsp; array&nbsp; ]}您将需要解析这些响应以获得所需的结果。建议修改&nbsp; &nbsp; function createDocument()&nbsp;{&nbsp;&nbsp;&nbsp; //var headers = Sheets.Spreadsheets.Values.get('fileID', 'A1:Z1');&nbsp; var studentHistory = Sheets.Spreadsheets.Values.get('fileID', 'A2:Z200');&nbsp; var templateId = 'fileID';&nbsp; var documentId;&nbsp; var dstFolder = DriveApp.getFolderById('folderID');&nbsp; var length = studentHistory.values.length;...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript