Google Apps Script document not accessible by replaceText()

后端 未结 1 827
青春惊慌失措
青春惊慌失措 2021-01-16 11:43

Here is my code and I can\'t figure out why replaceText() isn\'t working.

function createDoc(){
  var templateid = \"1jM-6Qvy47gQ45u88WfDU_RvfuSTsw27zBP_9Mf         


        
1条回答
  •  再見小時候
    2021-01-16 12:08

    Consider this:

    body.replaceText("%companyName%", "test1");
    

    That will look for every instance of "companyName" with "%" on either side of it. The "%" in this case is just that, a piece of punctuation in a strange place. This is a convention used to decrease the likelihood of accidentally replacing real text in the document.

    Your template document must have that exact pattern for the replacement to work. (Yours doesn't... instead you have just "companyName". Change it to "%companyName%".) Apply that rule for any other replacement you want to make.


    You could benefit from some optimization.

      ...
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getActiveSheet();
      // Next line is hard to maintain - there's a better way.
      // var data = sheet.getRange(2, 1, sheet.getLastRow()-1,sheet.getLastColumn()).getValues();
      // Read whole spreadsheet, skip headers
      var data = sheet.getDataRange().getValues().slice(1);
    
      // Already read in all data, use it instead of reading sheet again.
      var firstName = data[data.length-1][2-1];    // (2-1) because array counts from 0
      var lastName = data[data.length-1][3-1];     // while spreadsheet columns from 1
      var guestEmail = data[data.length-1][7-1];   // Better: put these into variables.
      ...
    

    While experimenting with your code, I ran into an autocompletion issue with doc.getActiveSection(). It turns out that there has been a recent change, according to the release notes for April 15 2013.

    Renamed Document.getActiveSection() to getBody().

    You should update your code accordingly.

    0 讨论(0)
提交回复
热议问题