Server error when trying to save range of Google sheet cells to PDF in Google Apps Script

huangapple go评论67阅读模式
英文:

Server error when trying to save range of Google sheet cells to PDF in Google Apps Script

问题

以下是代码的翻译部分:

// 我有以下代码,在 Google Apps Script 中运行。该代码尝试获取电子表格的某个范围并将其保存为 PDF 文件:

var range = sheet.getRange("A586:K" + lastRow);

// 获取当前活动的电子表格的 URL(链接)
var ss = SpreadsheetApp.getActiveSpreadsheet();

var pdfBlob = DriveApp.getFileById(ss.getId()).getAs('application/pdf');
pdfBlob.setName('test.pdf');

// 将 PDF 保存到您的 Google 云端硬盘。
var folder = DriveApp.getFolderById("1EJIzvW-oOnFfFOopiAAsudhbHsF5FGKz");
var file = folder.createFile(pdfBlob);

当我运行该代码时它运行了一段时间然后出现以下错误

抱歉服务器发生错误请稍等片刻然后重试
英文:

I have the following code, running in Google Apps Script. The code attempts to take a range of a spreadsheet and save it as a PDF:

var range = sheet.getRange("A586:K" + lastRow); 

// Get the currently active spreadsheet URL (link)
var ss = SpreadsheetApp.getActiveSpreadsheet();

var pdfBlob = DriveApp.getFileById(ss.getId()).getAs('application/pdf');
pdfBlob.setName('test.pdf');


// Save the PDF to your Google Drive.
var folder = DriveApp.getFolderById("1EJIzvW-oOnFfFOopiAAsudhbHsF5FGKz"); 
var file = folder.createFile(pdfBlob);

When I run the code, it runs for a while, then gives me the error:

We're sorry, a server error occurred. Please wait a bit and try again

Any tips on what might be going on?

答案1

得分: 1

以下是翻译的内容:

"我相信你的目标如下。

  • 你想使用Google Apps Script将工作表的特定范围导出为PDF格式。

在你的脚本中,range 没有被使用。并且整个工作表被导出。在这种情况下,我认为你的目标可以通过查询参数实现。参考链接 当这个参数在你的脚本中生效时,可以考虑以下修改。

修改后的脚本:

function myFunction() {
  const sheetName = "Sheet1"; // 请设置你的工作表名称。
  const folderId = "###";  // 请设置你的文件夹ID。

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName(sheetName);
  const range = sheet.getRange("A586:K" + sheet.getLastRow());
  const startRow = range.getRow() - 1;
  const endRow = startRow + range.getNumRows();
  const startCol = range.getColumn() - 1;
  const endCol = startCol + range.getNumColumns();
  const url = `https://docs.google.com/spreadsheets/d/${ss.getId()}/export?format=pdf&gid=${sheet.getSheetId()}&r1=${startRow}&c1=${startCol}&r2=${endRow}&c2=${endCol}`;
  const res = UrlFetchApp.fetch(url, { headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() } });
  const folder = DriveApp.getFolderById(folderId);
  folder.createFile(res.getBlob().setName("sample.pdf"));
}

参考资料:

英文:

I believe your goal is as follows.

  • You want to export the specific range of a sheet as a PDF format using Google Apps Script.

In your script, range is not used. And, the whole sheet is exported. In this case, I think that your goal can be achieved by the query parameter. Ref When this is reflected in your script, how about the following modification?

Modified script:

function myFunction() {
  const sheetName = "Sheet1"; // Please set your sheet name.
  const folderId = "###";  // Please set your folder ID.

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName(sheetName);
  const range = sheet.getRange("A586:K" + sheet.getLastRow());
  const startRow = range.getRow() - 1;
  const endRow = startRow + range.getNumRows();
  const startCol = range.getColumn() - 1;
  const endCol = startCol + range.getNumColumns();
  const url = `https://docs.google.com/spreadsheets/d/${ss.getId()}/export?format=pdf&gid=${sheet.getSheetId()}&r1=${startRow}&c1=${startCol}&r2=${endRow}&c2=${endCol}`;
  const res = UrlFetchApp.fetch(url, { headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() } });
  const folder = DriveApp.getFolderById(folderId);
  folder.createFile(res.getBlob().setName("sample.pdf"));
}

References:

huangapple
  • 本文由 发表于 2023年2月18日 07:34:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定