Javascript Google脚本,用于循环未按预期工作

Javascript Google脚本,用于循环未按预期工作,javascript,html,google-apps-script,Javascript,Html,Google Apps Script,我正在尝试创建一个动态googledoc,它使用%Address0%、%Address1%、%Address2%等从包含信息的电子表格中填充。我的当前代码适用于前%description0%的所有代码,但随后的%DeDescription1%和%description2%未填充。我猜我的for循环没有经过第一次迭代,或者由于某种原因,主体不能被修改两次,但是我在支持文档中找不到任何东西可以给我这个想法 此外,我排除了var数据出错的可能性,我可以一次获取所有属性,例如:我可以获取%Address

我正在尝试创建一个动态googledoc,它使用%Address0%、%Address1%、%Address2%等从包含信息的电子表格中填充。我的当前代码适用于前%description0%的所有代码,但随后的%DeDescription1%和%description2%未填充。我猜我的for循环没有经过第一次迭代,或者由于某种原因,主体不能被修改两次,但是我在支持文档中找不到任何东西可以给我这个想法

此外,我排除了var数据出错的可能性,我可以一次获取所有属性,例如:我可以获取%Address0%,%Type0%,%Price0%以正确填充,或者我可以获取%Address1%,%Type1%,%Price1%,但我无法让它们一次完成所有操作

// create a menu
function onOpen() {
  var menuEntries = [ {name: "Create CMA", functionName: "CreateCMA"}];
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.addMenu("CMA Generator", menuEntries);
}

function CreateCMA() {
  var templateid = "1uLC41M1B0KjtC_AlxFAT2h2_9HPLsLSk4HPhXgq2ebY"; // template file id
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var data = sheet.getRange(2,1,6,11).getValues(); // starting with row 2 and column 1 as our upper-left most column
  var docid = DriveApp.getFileById(templateid).makeCopy().getId();
  // Create our number formatter.
  var formatter = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD',
  });
  var doc = DocumentApp.openById(docid);

  for (var r = 0; r < 6; r++){
    var body = doc.getBody();
    var row = data[r];
    body.replaceText("%Address" + r + "%", row[0]);
    body.replaceText("%Type" + r + "%", row[1]);
    body.replaceText("%Price" + r + "%", formatter.format(row[2]));
    var date = row[3];
    var dateform = Utilities.formatDate(date, "+5", "dd MMM yyyy");
    body.replaceText("%Date" + r + "%", dateform);
    body.replaceText("%Built" + r + "%", row[4]);
    body.replaceText("%Square_ft" + r + "%", row[5]);
    body.replaceText("%Bedroom" + r + "%", row[6]);
    body.replaceText("%Bath" + r + "%", row[7]);
    body.replaceText("%Assessed_Val" + r + "%", formatter.format(row[8]));
    body.replaceText("%Initial_Ask" + r + "%", formatter.format(row[9]));
    var post = row[10];
    var postform = Utilities.formatDate(post, "+5", "dd MMM yyyy");
    body.replaceText("%Day_Posted" + r + "%", postform);
  }
    doc.saveAndClose();

}
//创建一个菜单
函数onOpen(){
var menuEntries=[{name:“创建CMA”,functionName:“创建CMA”}];
var ss=SpreadsheetApp.getActiveSpreadsheet();
ss.addMenu(“CMA生成器”,菜单);
}
函数CreateCMA(){
var templateid=“1uLC41M1B0KjtC_AlxFAT2h2_9hplsk4hphxgq2eby”;//模板文件id
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getActiveSheet();
var data=sheet.getRange(2,1,6,11).getValues();//以第2行和第1列作为最左上方的列开始
var docid=DriveApp.getFileById(templateid.makeCopy().getId();
//创建我们的数字格式化程序。
var formatter=new Intl.NumberFormat('en-US'{
风格:“货币”,
货币:美元,
});
var doc=DocumentApp.openById(docid);
对于(var r=0;r<6;r++){
var body=doc.getBody();
var行=数据[r];
正文.replaceText(“%Address”+r+“%”,第[0]行);
正文.replaceText(“%Type”+r+“%”,第[1]行);
body.replaceText(“%Price”+r+“%”,formatter.format(第[2]行));
var日期=第[3]行;
var dateform=实用程序.formatDate(日期,“+5”,“dd-MMM-yyyy”);
正文.replaceText(“%Date”+r+“%”,dateform);
正文.replaceText(“%build”+r+“%”,第[4]行);
正文.replaceText(“%Square_ft”+r+“%”,第[5]行);
body.replaceText(“%卧室”+r+“%”,第[6]行);
正文.replaceText(“%Bath”+r+“%”,第[7]行);
body.replaceText(“%Assessed_Val”+r+“%”,formatter.format(第[8]行));
body.replaceText(“%Initial_Ask”+r+“%”,formatter.format(第[9]行));
var post=第[10]行;
var postform=Utilities.formatDate(post,“+5”,“dd-MMM-yyyy”);
正文.replaceText(“%Day_Posted”+r+“%”,postform);
}
doc.saveAndClose();
}

尝试移动
var body=doc.getBody()外部循环谢谢你的拍摄,但我已经尝试过了。在循环内部添加
console.log(r)
看看它是否执行了6次,如果它给我这个错误。[20-04-18 16:17:50:392 PDT]异常:参数(字符串、字符串、字符串)与Utilities.formatDate的方法签名不匹配。在CreateCMA(代码:61:30)。我可能应该提到,我知道它抛出了一个错误,但在那之后它仍然至少执行一些代码,因为所有的%descriptions%都将填充。但是为了回答您原来的问题,记录器只打印出这个错误。
row[10]
在Google sheetstry中没有格式化为日期。
var body=doc.getBody()外部循环谢谢你的拍摄,但我已经尝试过了。在循环内部添加
console.log(r)
看看它是否执行了6次,如果它给我这个错误。[20-04-18 16:17:50:392 PDT]异常:参数(字符串、字符串、字符串)与Utilities.formatDate的方法签名不匹配。在CreateCMA(代码:61:30)。我可能应该提到,我知道它抛出了一个错误,但在那之后它仍然至少执行一些代码,因为所有的%descriptions%都将填充。但是为了回答您最初的问题,记录器只打印出这个错误。
行[10]
在谷歌表单中没有格式化为日期