首页 > 解决方案 > .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];
    
    //Make a copy of the template file
    documentId = DriveApp.getFileById(templateId).makeCopy(dstFolder).getId();
    
    //Change name of newly created document
    DriveApp.getFileById(documentId).setName('SocialHistory_' + studentName + '_' + date);

    var body = DocumentApp.openById(documentId).getBody();
    
    //Insert values
    body.replaceText('<<date>>', date);
    body.replaceText('<<studentName>>', studentName);
    body.replaceText('<<dob>>', dob);
    body.replaceText('<<pcDoctor>>', pcDoctor);
    body.replaceText('<<address>>', address);
    body.replaceText('<<fgName>>', fgName);
    body.replaceText('<<mgName>>', mgName);
    body.replaceText('<<phoneMom>>', phoneMom);
    body.replaceText('<<phoneDad>>', phoneDad);
    body.replaceText('<<empMom>>', empMom);
    body.replaceText('<<empDad>>', empDad);
    body.replaceText('<<livesWithe>>', livesWith);
    body.replaceText('<<childrenInHome>>', childrenInHome);
    body.replaceText('<<childrenNotInHome>>', childrenNotInHome);
    body.replaceText('<<othersInHome>>', othersInHome);
    body.replaceText('<<illnesses>>', illnesses);
    body.replaceText('<<illnessDetails>>', illnessDetails);
    body.replaceText('<<hospitalizations>>', hospitalizations);
    body.replaceText('<<hospDetails>>', hospDetails);
    body.replaceText('<<trauma>>', trauma);
    body.replaceText('<<traumaDetails>>', traumaDetails);
    body.replaceText('<<injuries>>', injuries);
    body.replaceText('<<injuryDetails>>', injuryDetails);
    body.replaceText('<<medications>>', medications);
    body.replaceText('<<additionalComments>>', additionalComments);
    body.replaceText('<<otherSchools>>', otherSchools);

    
  }

  
}

标签: javascriptarraysgoogle-apps-scriptgoogle-sheetsgoogle-sheets-api

解决方案


解决方案

我看到您正在使用高级 Google 服务来调用表格 API。这个 Apps Script 类允许您直接从您的脚本调用 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;

...

参考

谷歌表格 API

高级谷歌服务


推荐阅读