Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

javascript - 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('FileID');
  
  // 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('{{Page Name}}', row[1]);
    body.replaceText('{{Page URL}}', row[2]);
    body.replaceText('{{Primary Keyword}}', row[3]);
    body.replaceText('{{H1}}', row[7]);
    body.replaceText('{{Meta Title}}', row[8]);
    body.replaceText('{{Meta Description}}', row[10]);
    body.replaceText('{{Secondary Keyword 1}}', row[12]);
    body.replaceText('{{Secondary Keyword 2}}', row[13]);
    body.replaceText('{{Semantic Keyword 1}}', row[14]);
    body.replaceText('{{Semantic Keyword 2}}', row[15]);
    body.replaceText('{{Semantic Keyword 3}}', row[16]);
    body.replaceText('{{Semantic Keyword 4}}', row[17]);
    body.replaceText('{{Semantic Keyword 5}}', row[18]);
    body.replaceText('{{Semantic Keyword 6}}', row[19]);
    body.replaceText('{{Semantic Keyword 7}}', row[20]);
    body.replaceText('{{Semantic Keyword 8}}', row[21]);
    body.replaceText('{{Semantic Keyword 9}}', row[22]);
    body.replaceText('{{Semantic Keyword 10}}', row[23]);
    body.replaceText('{{Semantic Keyword 11}}', row[24]);
    body.replaceText('{{Semantic Keyword 12}}', 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)
    
  })
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...