'Google sheets script - multiple sheets to pdf script

I have made a script that exports a certain sheet to a PDF. But at this moment we want to add a second sheet, so we need to export 2 sheets to a single PDF .

Any idea how I can do that starting with the code I have now. (ps. in this code, I also copy info from one sheet to another, so we can do a calculation. This works perfectly, so that is not an issue.

function MakeFriendly(name) {
  // Makes spaces be underscores and . (dots) be eliminated.
  return name.replace(/ /g, '_').replace(/\./g, '');
}

function DateFormat(date) {
  // Formats something like: 4 apr. 2022 12:21
  return date.toLocaleString('nl-BE', { timeZone: 'CET', dateStyle: 'medium', timeStyle: 'short' });
}

function createblobpdf(source_spreadsheet_id, source_sheet_id, output_pdf_name) {
  const url = `https://docs.google.com/spreadsheets/d/${source_spreadsheet_id
  }/export`
    + '?format=pdf'
    + '&size=A4' // paper size legal / letter / A4
    + '&portrait=true' // orientation, false for landscape
    + '&scale=2' // 1= Normal 100% / 2= Fit to width / 3= Fit to height / 4= Fit to Page
    + '&fitw=true' // fit to width, false for actual size
    + '&top_margin=0.00' // All four margins must be set!
    + '&bottom_margin=0.00' // All four margins must be set!
    + '&left_margin=0.00' // All four margins must be set!
    + '&right_margin=0.00' // All four margins must be set!
    + '&sheetnames=false&printtitle=false' // hide optional headers and footers
    + '&pagenum=false&gridlines=false' // hide page numbers and gridlines
    + '&fzr=true' // do not repeat row headers (frozen rows) on each page
    + '&horizontal_alignment=CENTER' // LEFT/CENTER/RIGHT
    + '&vertical_alignment=TOP' // TOP/MIDDLE/BOTTOM
    + `&gid=${source_sheet_id}`; // the sheet's Id
  const token = ScriptApp.getOAuthToken();
  // request export url
  const response = UrlFetchApp.fetch(url, {
    headers: {
      Authorization: `Bearer ${token}`,
    },
  });
  const theBlob = response.getBlob().setName(output_pdf_name);
  return theBlob;
}

function Copytocalculator() {
  // get sheets
  const outputwebsite = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/195bSouHEPYbI5Nkd7FsfGoyUjaTeK8AbPoe0f4TQtd4/edit#gid=1395142221').getSheetByName('Offerte_aanvragen_calculatie');
  const outputwebsite_original = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/195bSouHEPYbI5Nkd7FsfGoyUjaTeK8AbPoe0f4TQtd4/edit#gid=1395142221').getSheetByName('Offerte_aanvragen');

  // Load data
  const data = outputwebsite.getDataRange().getValues();

  // Keyword for marking rows as done
  const DONE_KEYWORD = 'EntryAlreadyProcessedByScript';

  // Useful columns
  const TIMESTAMP_COL = 0; // Column named "Submission Date"
  const FIRST_NAME_COL = 1; // Column named "Voornaam"
  const LAST_NAME_COL = 2; // Column named "Achternaam"
  const EMAIL_COL = 3; // Column named "E-mail"
  const STREET_NAME_COL = 6; // Column named "Straat en huisnummer"
  const CITY_NAME_COL = 7; // Column named "Plaats"
  const POSTAL_CODE_COL = 9; // Column named "Postcode"
  const OFFER_NUMBER = 33; // Column named "offerte nummer"

  const ELECTRICITY_USAGE_COL = 15; // Column named "Wat is jouw jaarlijks elektriciteitsverbruik?"

  // Get last column
  let last_column = 0;
  for (var col = 0; col < data[0].length; col++) {
    // We search for the submission ID because the form can change in the future and this is trusty point of reference.
    if (data[0][col] == 'Submission ID') {
      last_column = col;
      
      break;
    }
  }

  // Start in row 1, row 0 is headers and should already be processed
  for (let row = 1; row < data[0].length; row++) {
    // Process each row and skip the ones already processed
    Logger.log(data[row][last_column + 1])
    if (data[row][last_column + 1] == DONE_KEYWORD) {
      Logger.log(`Skiping row ${row} as it is already done.`);
      continue;
    
    }

    // Skip any rows that look empty
    if (data[row][0] == '') {
       Logger.log(`Skiping row ${row} as it looks empty.`);
       data[row-1][last_column+1]=DONE_KEYWORD
      continue;
    }
    

    // Create a transposed copy of the data.
    const values = [];
    for (var col = ELECTRICITY_USAGE_COL, new_col = 0; col < last_column-1; col++, new_col++) {
      values[new_col] = [data[row][col]];
    }
    
    
    // Get the additional info from our sheet
    const clientfirstname = data[row][FIRST_NAME_COL];
    const clientlastname = data[row][LAST_NAME_COL];
    const timestamp = data[row][TIMESTAMP_COL];
    const clientstreetname = data[row][STREET_NAME_COL];
    const clientemail = data[row][EMAIL_COL];
    const clientcityname = data[row][CITY_NAME_COL];
    const clientstpostalcode = data[row][POSTAL_CODE_COL];
    const offernumber = data[row][OFFER_NUMBER];
    

    // Create destination spreadsheet. Make a friendly name for it.
    const sheetname = `offerte_${MakeFriendly(clientfirstname)}_${MakeFriendly(clientlastname)}_${MakeFriendly(DateFormat(timestamp))}`;

    // Make a copy of the template file
    const destFolder = DriveApp.getFolderById('1pbTE4xlMo95U-pJg27Upk95d3nMGDWnM');
    const documentId = DriveApp.getFileById('1m74KTqZYTdyXa_FpX1cf4dhFHSf0Y4k7ycv-vo_XS2E').makeCopy(destFolder).getId();

    // Rename the copied file
    DriveApp.getFileById(documentId).setName(sheetname);
    const newfile = SpreadsheetApp.openById(documentId).getSheetByName('input_website');

    // Run values through the calculation spreadsheet
    newfile.getRange('B7:B24').setValues(values);
    newfile.getRange('B2').setValue(`${clientfirstname} ${clientlastname}`);
    newfile.getRange('B3').setValue(clientstreetname);
    newfile.getRange('B4').setValue(clientcityname);
    newfile.getRange('B5').setValue(clientstpostalcode);
    newfile.getRange('B6').setValue(clientemail);
    newfile.getRange('D1').setValue(offernumber);
   

     SpreadsheetApp.flush();  // Flush content changes
     const invoice_template_id = SpreadsheetApp.openById(documentId).getSheetByName('Invoice_Template').getSheetId();

      //  folder = DriveApp.getFolderById('ID');

     const theBlob = createblobpdf(documentId, invoice_template_id, sheetname);
     const folder = DriveApp.getFolderById('1tDqHKnNYHuE02Ppwqv1mQcwmL5oSmmQp');
      folder.createFile(theBlob);
     Logger.log(`Created pdf for row ${row}.`);

     outputwebsite.getRange(row+1,last_column+2).setValue(DONE_KEYWORD)
  }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source