Automatic google spreedshet page pdf save in google drive

MAHBUBUL ALAMMAHBUBUL ALAM
1 min read
function onOpen() {
  const ui = SpreadsheetApp.getUi();
  ui.createMenu('MENU')
      .addItem('PDF','makePDF')
      .addItem('Clear Routine','clear')
      .addToUi();
}

function clear() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRangeList(['B5:B100', 'C5:E5', 'F5:H100']).activate()
  .clear({contentsOnly: true, skipFilteredRows: true});
};

//How To Save Data In Google Sheets With Timestamps Using Apps Script
function makePDF() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var token = ScriptApp.getOAuthToken();
  var sheet = ss.getSheetByName("Routine");

  var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId());
  var folderID = "13nK_9GfP0ehVWool__Px-FBtIdGoe7Cr"; // Folder id to save in a folder.
  var folder = DriveApp.getFolderById(folderID);
  var invoiceNumber = ss.getRange("G2").getValue();
 // var invoiceCusName = ss.getRange("C1").getValue();
  var pdfName = invoiceNumber;

  var url_ext = 'exportFormat=pdf&format=pdf'        // export as pdf / csv / xls / xlsx
    + '&size=A4'                       // paper size legal / letter / A4
    + '&portrait=true'                    // orientation, false for landscape
    + '&fitw=true&source=labnol'           // fit to page width, false for actual size
    + '&sheetnames=false&printtitle=false' // hide optional headers and footers
    + '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
    + '&fzr=false'                         // do not repeat row headers (frozen rows) on each page
    + '&gid=';                             // the sheet's Id

  var response = UrlFetchApp.fetch(url + url_ext + sheet.getSheetId(), {
    headers: {
      'Authorization': 'Bearer ' + token
    }
  });

  var blob = response.getBlob().setName(pdfName + '.pdf');
  folder.createFile(blob);
}
0
Subscribe to my newsletter

Read articles from MAHBUBUL ALAM directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

MAHBUBUL ALAM
MAHBUBUL ALAM

Hi, I am Full Stack Developer. I can build web application, android application, desktop application. My core programming language is JavaScript.