簡體   English   中英

.length 不適用於 Google Apps 腳本中的數組

[英].length not working on array in Google Apps Script

我有這個代碼。 我想遍歷數組並為每個條目創建一個新文檔。 如果我手動將循環長度設置為行數,則它工作正常。 我想將它設置為循環數組的長度。 但是 .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);

    
  }

  
}

studentHistory.values是數組。

因此,試試這個來獲得長度:

var length = studentHistory.values.length;

解決方案

我看到您正在使用高級 Google 服務來調用 Sheets 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

高級 Google 服務

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM