繁体   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