2021年1月22日星期五

How Do You Use App Script to insert Blank Placeholder Data for Missing Cell Contents?

I have quite a few data inputs in a Google Sheet that are feeding a Google Doc via placeholders using App Scripts. When I run the below function I get the following error message "Exception: Invalid argument: replacement"

I believe that because a few of my replacetext calls have blank cells it doesn't replace them in the Google Doc.

What is required to replace the placeholder text in the Google Doc even if the contents of the cell are blank?

function CreateRecommendations() {    // This value should be the id of your document template that we created in the last step      const googleDocTemplate = DriveApp.getFileById(FfileID');        // This value should be the id of the folder where you want your completed documents stored    const destinationFolder = DriveApp.getFolderById('FolderID')    // Here we store the sheet as a variable    const sheet = SpreadsheetApp      .getActiveSpreadsheet()      .getSheetByName('Blueprint')        // Now we get all of the values as a 2D array    const rows = sheet.getDataRange().getValues();        // Start processing each spreadsheet row    rows.forEach(function(row, index){      // Here we check if this row is the headers, if so we skip it      if (index === 0) return;      // Here we check if a document has already been generated by looking at 'Document Link', if so we skip it      if (row[0]) return;            //Using the row data in a template literal, we make a copy of our template document in our destinationFolder      const copy = googleDocTemplate.makeCopy(`Recommendations: ${row[1]}` , destinationFolder)      //Once we have the copy, we then open it using the DocumentApp      const doc = DocumentApp.openById(copy.getId())      //All of the content lives in the body, so we get that for editing      const body = doc.getBody();                //In these lines, we replace our replacement tokens with values from our spreadsheet row      body.replaceText('', row[1]);      body.replaceText('', row[2]);      body.replaceText('', row[3]);      body.replaceText('', row[7]);      body.replaceText('', row[8]);      body.replaceText('', row[10]);      body.replaceText('', row[12]);      body.replaceText('', row[13]);      body.replaceText('', row[14]);      body.replaceText('', row[15]);      body.replaceText('', row[16]);      body.replaceText('', row[17]);      body.replaceText('', row[18]);      body.replaceText('', row[19]);      body.replaceText('', row[20]);      body.replaceText('', row[21]);      body.replaceText('', row[22]);      body.replaceText('', row[23]);      body.replaceText('', row[24]);      body.replaceText('', row[25]);        //We make our changes permanent by saving and closing the document      doc.saveAndClose();      //Store the url of our new document in a variable      const url = doc.getUrl();      //Write that value back to the 'Document Link' column in the spreadsheet.       sheet.getRange(index + 1, 1).setValue(url)          })  }  
https://stackoverflow.com/questions/65855897/how-do-you-use-app-script-to-insert-blank-placeholder-data-for-missing-cell-cont January 23, 2021 at 12:59PM

没有评论:

发表评论