簡體   English   中英

我正在嘗試在 email 中將谷歌表格中的范圍作為 pdf 發送。 我的腳本卡在 getid()

[英]I am trying to send a range in google sheets as a pdf in an email. My script is getting stuck on getid()

我正在嘗試在 Google 腳本中創建一個宏,該宏會向我發送我創建的工作表的 email。 當我嘗試運行它時,腳本卡在 function GetSheetID() 處。 (第 2 個函數的第 4 行)

“TypeError:無法讀取未定義的屬性“getSheetId”(第 51 行,文件“宏”)”

我也對其他 email 技術持開放態度。 我的主要目標是在 email 中獲取一個范圍並作為圖片或 PDF 發送。

function sendSheetToPdfwithA1MailAdress(){ // this is the function to call
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheets()[4]; // 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 shName = 4 //sh.getName()
  
  var shNum = 4
  var shRng = 'A1:R35'
  var pdfName = 'Automated Snapshot'
  var email = 'email@gmail.com'
  var subject = 'Daily Snapshot'
  var htmlbody = ''
  mailPdf(shNum,shRng,pdfName,email,subject,htmlbody);
}

function mailPdf(shNum,shRng,pdfName,email,subject,htmlbody) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ssId = ss.getId();
  var shId = shNum ? ss.getSheets()[shNum].getSheetId() : null;  
  var url_base = ss.getUrl().replace(/edit$/,'');
  var url_ext = 'export?exportFormat=pdf&format=pdf'   //export as pdf
      + (shId ? ('&gid=' + shId) : ('&id=' + ssId))
      + (shRng ? ('&range=E1:L25') : null)
      + '&format=pdf'                   //export format
      + '&size=letter'                      //A3/A4/A5/B4/B5/letter/tabloid/legal/statement/executive/folio
      //+ '&portrait=false'               //true= Portrait / false= Landscape
      //+ '&scale=1'                      //1= Normal 100% / 2= Fit to width / 3= Fit to height / 4= Fit to Page
      //+ '&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!
      + '&gridlines=false'              //true/false
      //+ '&printnotes=false'             //true/false
      //+ '&pageorder=2'                  //1= Down, then over / 2= Over, then down
      //+ '&horizontal_alignment=CENTER'  //LEFT/CENTER/RIGHT
      + '&vertical_alignment=TOP'       //TOP/MIDDLE/BOTTOM
      //+ '&printtitle=false'             //true/false
      //+ '&sheetnames=false'             //true/false
      //+ '&fzr=false'                    //true/false frozen rows
      //+ '&fzc=false'                    //true/false frozen cols
      //+ '&attachment=false'             //true/false

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

  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 + "," + Session.getActiveUser().getEmail() // use this to email self and others
      email,                                              // use this to only email users requested
      subject+' (' + pdfName +')', 
      'html content only', 
      mailOptions);

  }
}

關於

“TypeError:無法讀取未定義的屬性“getSheetId”(第 51 行,文件“宏”)”

您很可能正在使用以一為基數的索引而不是從零開始的索引,並且您的電子表格中的工作表少於 5 張。

修復取決於您要將哪個工作表 ID 分配給以下代碼行的shId

var shId = shNum ? ss.getSheets()[shNum].getSheetId() : null; 

參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM