Javascript .length不适用于Google应用程序脚本中的数组
我有这个密码。我想在数组中循环并为每个条目创建一个新文档。如果我手动将循环长度设置为行数,则效果良好。我想将其设置为数组长度的循环。但是.length属性始终返回null。我错过了什么。我也尝试过每一个循环,但没有运气Javascript .length不适用于Google应用程序脚本中的数组,javascript,arrays,google-apps-script,google-sheets,google-sheets-api,Javascript,Arrays,Google Apps Script,Google Sheets,Google Sheets Api,我有这个密码。我想在数组中循环并为每个条目创建一个新文档。如果我手动将循环长度设置为行数,则效果良好。我想将其设置为数组长度的循环。但是.length属性始终返回null。我错过了什么。我也尝试过每一个循环,但没有运气 function createDocument() { //var headers = Sheets.Spreadsheets.Values.get('fileID', 'A1:Z1'); var studentHistory = Sheets.Sprea
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);
}
}
函数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');
变量长度=studentHistory.length;
Logger.log(studentHistory);
Logger.log(长度);
//在工作表中的行中循环
对于(var i=0;i
studentHistory。值是数组
因此,请尝试以下方法获取长度:
var length = studentHistory.values.length;
studentHistory.values
是数组
因此,请尝试以下方法获取长度:
var length = studentHistory.values.length;
解决方案
我看到您正在使用高级Google服务来调用Sheets API。此应用程序脚本类允许您直接从脚本调用Google API,并自动处理授权过程
但是,它不能像包装器中的内置类那样工作
您的请求将返回以下类似HTTP的响应:
您将需要解析这些响应以获得所需的结果
拟议的修改
参考文献
解决方案
我看到您正在使用高级Google服务来调用Sheets API。此应用程序脚本类允许您直接从脚本调用Google API,并自动处理授权过程
但是,它不能像包装器中的内置类那样工作
您的请求将返回以下类似HTTP的响应:
您将需要解析这些响应以获得所需的结果
拟议的修改
参考文献
就是这样。谢谢你的解释,就这样。谢谢你的解释。
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;
...