Search code examples
google-sheetsgoogle-apps-scriptgoogle-docs

Replacing text in header of google docs


Hi I'm using google sheets to create a quote by replacing text in a google doc. This works well in the body of the document, but not in the header. The code im using is given below.

function onOpen() {
  const ui = SpreadsheetApp.getUi();
  const menu = ui.createMenu('Quote Menu'); // Wrap the menu name in quotes
  menu.addItem('Create EN/IT Quote', 'createNewGoogleDocs');
  menu.addItem('Create EN + IT Quote', 'createNewGoogleDocs');
  menu.addItem('Create EN Quote', 'createNewGoogleDocs');
  menu.addItem('Create IT Quote', 'createNewGoogleDocs');
  menu.addToUi(); // Add parentheses to call the addToUi function
}

function createNewGoogleDocs() {
  const googleDocTemplate = DriveApp.getFileById('file id');
  const destinationFolder = DriveApp.getFolderById('1folder id');
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Quote Data');
  const rows = sheet.getDataRange().getDisplayValues();
  


  rows.forEach(function(row, index){
    if (index === 0) return;

    const copy = googleDocTemplate.makeCopy(`Offer ${row[11]}. ${row[12]} - ${row[3]}` , destinationFolder)
    const doc = DocumentApp.openById(copy.getId())
    const body = doc.getBody();
    const header = doc.getHeader();
    
    
    //In these lines, we replace our replacement tokens with values from our spreadsheet row
    
    header.replaceText('{{EN12}}', row[11]) 
    body.replaceText('{{EN1}}', row[0]);
    body.replaceText('{{EN2}}', row[1]);
    body.replaceText('{{EN3}}', row[2]);
    body.replaceText('{{EN4}}', row[3]);

  
    //We make our changes permanent by saving and closing the document
    doc.saveAndClose();
  })
}

add more details and more charchters


Solution

  • I thought that your script header.replaceText('{{EN12}}', row[11]) can be used to the header. But, from but not in the header, I guessed that you might have enabled the 1st-page header. Because when the 1st page header is enabled, header.replaceText('{{EN12}}', row[11]) cannot be used. If my guess is correct, how about the following modification?

    Modified script:

    Please modify createNewGoogleDocs() as follows.

    function createNewGoogleDocs() {
      const googleDocTemplate = DriveApp.getFileById('file id');
      const destinationFolder = DriveApp.getFolderById('1folder id');
      const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Quote Data');
      const rows = sheet.getDataRange().getDisplayValues();
      rows.forEach(function (row, index) {
        if (index === 0) return;
        const copy = googleDocTemplate.makeCopy(`Offer ${row[11]}. ${row[12]} - ${row[3]}`, destinationFolder);
        const doc = DocumentApp.openById(copy.getId());
        const body = doc.getBody();
    
        // --- I modified the below script.
        const document = body.getParent();
        let s = null;
        while (s = document.findElement(DocumentApp.ElementType.HEADER_SECTION, s)) {
          s.getElement().asHeaderSection().replaceText('{{EN12}}', row[11]);
        }
        // ---
    
        body.replaceText('{{EN1}}', row[0]);
        body.replaceText('{{EN2}}', row[1]);
        body.replaceText('{{EN3}}', row[2]);
        body.replaceText('{{EN4}}', row[3]);
        doc.saveAndClose();
      });
    }
    
    • When this script is run, replaceText('{{EN12}}', row[11]) can be used with both the 1st page header and other page headers.