【问题标题】:How can I generate and open pdf as a link with google sheet script?如何生成并打开 pdf 作为与 google sheet 脚本的链接?
【发布时间】:2021-02-04 08:10:54
【问题描述】:

我在谷歌电子表格中做一个发票程序,在程序中我使用图纸作为按钮(创建新发票、增加/减少发票编号和清除数据)。这些功能运行良好,但我还想添加一个按钮,该按钮将创建一个 PDF 文件并在 Web 浏览器中作为选项卡打开。到目前为止,我必须将 Invoice-sheet 复制为 tempSheet,然后删除按钮,然后下载 tempSheet 的 pdf。我在下面找到了这段代码,我试图更改它以使其适合我的程序(原始程序仅适用于活动工作表)。我认为问题可能出在我使用 getId() 和 getSheetId() 时。

  1. 如果我在 url 中使用 ss.getId(),我只会将活动表(发票表)作为 pdf 格式。
  2. 如果我使用 tempSheet.getId(),我会得到一个错误(ERROR TypeError: tempSheet.getId is not a function)

你知道我做错了什么吗?如果我理解正确,所有工作表的 ID 都是相同的,然后不同的工作表也有自己的 ID,我可以从 getSheetId() 获得。

getId()- docs.google.com/spreadsheets/d/ 后面的数字

getSheetId() - #gid=xxxxxxxxx

function createPDF(){
  //create a temporary sheet

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  
  var tempSpreadsheet = SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet();

  SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet('temp');
  
  var tempSheet = SpreadsheetApp.getActive().getSheetByName('temp');
  var invoiceNbr = tempSheet.getRange('C3').getValue().toString();
  var tempId = tempSheet.getId(); //ERROR TypeError: tempSheet.getId is not a function
  var tempSheetId = tempSheet.getSheetId();
  var drawings = tempSheet.getDrawings();
  for (var i = 0; i < drawings.length; i++) {
    drawings[i].remove();
  }

  const url = 'https://docs.google.com/spreadsheets/d/{ID}/export?'.replace('{ID}', tempId);
  
  const exportOptions =
    'exportFormat=pdf&format=pdf' + // export as pdf
    '&size=letter' + // paper size letter / You can use A4 or legal
    '&portrait=true' + // orientation portal, use false for landscape
    '&fitw=false' + // fit to page width false, to get the 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=' + tempSheetId; // the sheet's Id. Change it to your sheet ID.
  // You can find the sheet ID in the link bar. 
  // Select the sheet that you want to print and check the link,
  // the gid  number of the sheet is on the end of your link.
  
  var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
  var blob = UrlFetchApp.fetch(url + exportOptions, params).getBlob().setName(invoiceNbr+'.pdf');
  

  var pdfFile = DriveApp.createFile(blob);

  var downloadLink = HtmlService
    .createHtmlOutput('<p>Download your file <a href="' + pdfFile.getUrl() + '" target="_blank">here</a>.</p>')
    .setWidth(200)
    .setHeight(100);

  SpreadsheetApp.getUi().showModalDialog(downloadLink, "Download PDF");
  

  ss.deleteSheet(tempSheet);

【问题讨论】:

标签: google-apps-script google-sheets


【解决方案1】:

getSheetByName('temp')之前添加SpreadsheetApp.flush():

SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet('temp');

SpreadsheetApp.flush();
  
var tempSheet = SpreadsheetApp.getActive().getSheetByName('temp');

相关

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    相关资源
    最近更新 更多