编辑它后发送谷歌片为PDF

时间:2019-02-02 20:18:03

标签: javascript google-apps-script google-sheets

我正在尝试使用script.existing现有代码将pdf格式的google工作表通过电子邮件发送给我。但是问题是,在发送之前,脚本将编辑工作表,当以pdf格式发送时,这些更改无法捕获。有人可以帮我吗?

function sendSheetToPdfwithA1MailAdress(){ 
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheets()[0]; 
  var shName = sh.getName()

  sh.getRange("C5:G28").setValue("LLL");


  Utilities.sleep(5000)
  sendSpreadsheetToPdf(0, shName, "*********@gmail.com","test mail ", "Test!");
}
function sendSpreadsheetToPdf(sheetNumber, pdfName, email,subject, htmlbody) {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var spreadsheetId = spreadsheet.getId()  
  var sheetId = sheetNumber ? spreadsheet.getSheets()[sheetNumber].getSheetId() : null;  
  var url_base = spreadsheet.getUrl().replace(/edit$/,'');

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

     + (sheetId ? ('&gid=' + sheetId) : ('&id=' + spreadsheetId)) 

     + '&size=A4'      
     + '&portrait=true'    
     + '&fitw=true'        
     + '&sheetnames=true&printtitle=false&pagenumbers=true'  
     + '&gridlines=false'  
     + '&fzr=false';       
 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
   }
MailApp.sendEmail(
  email, 
  subject+" (" + pdfName +")", 
  "html content only", 
  mailOptions);

}
  }

1 个答案:

答案 0 :(得分:0)

在致电SpreadsheetApp.flush();之前添加spreadsheetToPdf