【问题标题】:Google Apps Script of merging Google Sheet data to Google Doc template is not iterating after the 1st row of data将 Google Sheet 数据合并到 Google Doc 模板的 Google Apps 脚本在第一行数据之后没有迭代
【发布时间】:2021-01-18 23:12:36
【问题描述】:

参考/测试 Google Drive 文件夹:

https://drive.google.com/drive/folders/1hPKQk7eRjSdlMDjiZI2BZrHIRhzVtYG5?usp=sharing

我有一个包含 1 个 Google Sheet 和 7 个 Google Doc 模板的文件夹。 Google Doc 模板具有占位符,用于填充来自 Google 表格的“PrintThis”选项卡/表格的数据。当我运行我的 Google Apps 脚本时,它会正确地在同一驱动器文件夹中制作模板的 Google Doc 副本,其中包含第 4 行中找到的第一行数据所需的正确值。其余 2 行未迭代,也没有复制/为这些行创建文件。此外,当我运行我的代码Exception: The document is inaccessible. Please try again later. (line 57, file "Code") 第 57 行是这一行时,我得到一个错误:var body = DocumentApp.openById(documentId).getBody(); 对于测试文件夹,我让所有有链接的人都可以编辑所有文件/文件夹,所以我不知道它为什么会给出我一个无法访问的错误。

这是我使用的 Google App 脚本代码:

function createDocument() {
  var headers = Sheets.Spreadsheets.Values.get('1xSWskGS8B_3Y35I4ycAjFZQiGbfJo13O3837RKnxnpk', 'PrintThis!A3:J3');
  var tactics = Sheets.Spreadsheets.Values.get('1xSWskGS8B_3Y35I4ycAjFZQiGbfJo13O3837RKnxnpk', 'PrintThis!A4:J5');
  var templateR1Id = '1s6T9CEmvf6VW4X-Df0jC8SrnVflAhSyF';
  var templateR3Id = '1zpeebLSo4F2Csi7K2zRyLdSchZuBhG6H';
  var templateR9Id = '1edgQkTsZSDjswM577S7CEsARnd_5ohLB';
  var templateCO6Id = '1sY8sY_P4NsRpXhrFqmheo4aSXtEbceTk';
  var templateCO9Id = '1l78q9lUEunoS8imcWhqP7Ly91E6SYSVZ';
  var templateCO12Id = '1kuCTdzQkEdtOoWq_1RgD3TYs3pBJUKFn';
  var templateCO13Id = '1yuCqS_aLnphHCNIITflf15IDDZbeV2T7';
  
  for(var i = 0; i < tactics.values.length; i++){
    var patient = tactics.values[i][1];
    var dueDate = tactics.values[i][2];
    var bereaved = tactics.values[i][4];
    var monthDue = tactics.values[i][3];
    var address = tactics.values[i][5];
    var city = tactics.values[i][6] ;
    var state = tactics.values[i][7];
    var zip = tactics.values[i][8] ;
    var agency = tactics.values[i][9];
    var mrnum = tactics.values[i][0];
    
    var templatemonth;
    if (monthDue == '6th Mo Due' && agency == 'CO Agency'){
      templatemonth = templateCO6Id}
    else if (monthDue == '9th Mo Due' && agency == 'CO Agency'){
      templatemonth = templateCO9Id}
    else if (monthDue == '12th Mo Due' && agency == 'CO Agency'){
      templatemonth = templateCO12Id}
    else if (monthDue == '13th Mo Due' && agency == 'CO Agency'){
      templatemonth = templateCO13Id}
    else if (monthDue == '1st Mo Due' && (agency == 'R Agency' || agency == 'R2 Agency')){
      templatemonth = templateR1Id}
    else if (monthDue == '3rd Mo Due' && (agency == 'R Agency' || agency == 'R2 Agency')){
      templatemonth = templateR3Id}
    else if (monthDue == '6th Mo Due' && (agency == 'R Agency' || agency == 'R2 Agency')){
      templatemonth = templateR6Id}    
    else if (monthDue == '9th Mo Due' && (agency == 'R Agency' || agency == 'R2 Agency')){
      templatemonth = templateR9Id}     
    
    //Make a copy of the template file
    var documentId = DriveApp.getFileById(templatemonth).makeCopy().getId();
    
    //Rename the copied file
    DriveApp.getFileById(documentId).setName(mrnum + '_' + monthDue);
    
    //Get the document body as a variable
    var body = DocumentApp.openById(documentId).getBody();
    
    
    //Insert the supplier name
    body.replaceText('##Bereaved##', bereaved)
    body.replaceText('##Address##', address)
    body.replaceText('##City##', city)
    body.replaceText('##State##', state)
    body.replaceText('##Zip##', zip)
    body.replaceText('##Letter Due Date##', dueDate)
    
  }

}

【问题讨论】:

    标签: javascript google-apps-script google-sheets google-sheets-api google-docs-api


    【解决方案1】:

    var tactics=Sheets.Spreadsheets.Values.get('1xSWskGS8B_3Y35I4ycAjFZQiGbfJo13O3837RKnxnpk', 'PrintThis!A4:J');

    我们使用 :J 而不是 J6 来包含所有可以是动态数量的行。还要确保模板是 Google Docs 格式,而不是 Microsoft Word 或任何其他文本文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      • 1970-01-01
      相关资源
      最近更新 更多