How to include a specific range only in a pdf exported from a Google Sheets spreadsheet

旧巷老猫 提交于 2020-07-23 06:33:37

问题


I have a sheet that I would like to send a specific range (A1:O44) as a pdf document and leave out further information in other cells. Currently, it is all functioning to send these sheets and all is working fine, however, I cannot seem to get the script correct to limit it to a certain range in the sheet.

There are 2 tabs in the sheet and I would like both of those to be sent in one email as part of the same attachment (as is currently set up). The email is generated by clicking the submit button from the custom submit menu on the top:

https://docs.google.com/spreadsheets/d/1EFJKr9281PND_h8TXNeumaCXb8KWLJQw9Itn0i_AZmI/edit#gid=1494149448

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('SUBMIT')
      .addItem('SUBMIT', 'menuItem1')
      .addToUi();
}

function menuItem1() {
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
     sendSheetToPdfwithA1MailAdress();
  
}

function sendSheetToPdfwithA1MailAdress(){ // this is the function to call
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName("TEAM1"); // it will send sheet 0 which is the first sheet in the spreadsheet.
  // if you change the number, change it also in the parameters below
  var practiceName = sh.getRange('E2').getValue()
  var startDate = sh.getRange('I4:L5').getValue();
  var emailBody = sh.getRange('I45').getValue();
  var staffName = sh.getRange('c43').getValue()
var shName = Utilities.formatString('%s %s',practiceName, startDate)
var message = "<HTML><BODY>"
        + "<P>Hi,"
        + "<br /><br />"
        + " Please find attached the payroll info for " + practiceName + " for the period " + startDate + "."
        + "<br /><br />" 
        + "" + emailBody + "."
        + "<br /><br />" 
        + " If you have any question please let me know"
        + "<br /><br />"
        + "Thanks, " + staffName + "."
        + "<br /><br />"
        + "</HTML></BODY>";
  sendSpreadsheetToPdf(0, shName, sh.getRange('A46').getValue(),"Payroll Summary", " " + message + " ");
}
function sendSpreadsheetToPdf(sheetNumber, pdfName, email,subject, htmlbody) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName("TEAM1");
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var spreadsheetId = spreadsheet.getId()  
  var sheetId = sheetNumber ? spreadsheet.getSheets()[sheetNumber].getSheetId() : null;  
  var url_base = spreadsheet.getUrl().replace(/edit$/,'');
  var email2 = sh.getRange('A47').getValue();
  var email3 = sh.getRange('A48').getValue();

  var url_ext = 'export?exportFormat=pdf&format=pdf'   //export as pdf

      + (sheetId ? ('&gid=' + sheetId) : ('&id=' + spreadsheetId)) 
      // following parameters are optional...
      + '&size=A4'      // paper size
      + '&portrait=false'    // orientation, false for landscape
      + '&fitw=true'        // fit to width, false for actual size
      + '&sheetnames=true&printtitle=false&pagenumbers=true'  //hide optional headers and footers
      + '&gridlines=false'  // hide gridlines
      + '&fzr=false';       // do not repeat row headers (frozen rows) on each page

  var options = {
    headers: {
      'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken(),
    }
  }

  var response = UrlFetchApp.fetch(url_base + url_ext, options); 
  var blob = response.getBlob().setName(pdfName + '.pdf');
  if (email) {
    var mailOptions = {
      attachments:blob, htmlBody:htmlbody, email2:email2 
    }
MailApp.sendEmail(
      email, 
      subject+" (" + pdfName +")", 
      "html content only",  
      mailOptions);
    
MailApp.sendEmail(
      email2, 
      subject+" (" + pdfName +")", 
      "html content only",  
      mailOptions);

MailApp.sendEmail(
      email3, 
      subject+" (" + pdfName +")", 
      "html content only",  
      mailOptions);
  }
}


回答1:


Disclaimer: I don't believe the use of query parameters to control the PDF export is officially supported by Google, but this GitHub Gist from Spencer-Easton has the info you need to accomplish your goal.

The issue is in the query parameters stored in url_ext.

You must include the gid, which you can get from the sheet's URL from the sheet editor (/edit#gid=NUMBERS).

You need to add to url_ext:

  • gid=NUMBERS (look in URL to get numbers)
  • ir=false (unknown what it refers to)
  • ic=false (unknown what it refers to)
  • r1=0 (starting row, zero index)
  • c1=0 (starting col, zero index)
  • r2=43 (end row, note change of index)
  • c2=14 (end col, note change of index)

I added those and was able to email just the selected portions of the selected sheet.



来源:https://stackoverflow.com/questions/62861704/how-to-include-a-specific-range-only-in-a-pdf-exported-from-a-google-sheets-spre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!