Search code examples
javascriptgoogle-apps-scriptgoogle-sheetsgoogle-sheets-apigoogle-docs-api

Google Apps Script of merging Google Sheet data to Google Doc template is not iterating after the 1st row of data


Reference/Test Google Drive folder:

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

I have a folder with 1 Google Sheet and 7 Google Doc templates. The Google Doc templates has placeholders for where data from the "PrintThis" tab/sheet of the Google Sheet will populate. When I run my Google Apps Script, it correctly makes a Google Doc copy of the template in the same drive folder with correct values needed for the 1st row of data found in row 4. The remaining 2 rows are not iterated through and no copy/file for these rows are made. Also I get an error when I run my code Exception: The document is inaccessible. Please try again later. (line 57, file "Code") Line 57 is this line: var body = DocumentApp.openById(documentId).getBody(); For the test folder, I made all the files/folder editable by anyone who has the link, so I dont know why it is giving me an inaccessible error.

Here is my the Google App Script code that I used:

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)
    
  }

}

Solution

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

    We use :J, instead of J6 to include all rows which can be a dynamic amount. Also make sure the templates are in Google Docs format and not Microsoft Word or any other text file.