【问题标题】:google-app-script how to cocat all blob into one blobgoogle-app-script 如何将所有 blob 连接成一个 blob
【发布时间】:2022-11-26 11:33:38
【问题描述】:
var res1 = UrlFetchApp.fetch(url1,para);
var res2 = UrlFetchApp.fetch(url2,para)

上面的 url1&url2 是从 google sheet 导出 pdf 的链接。如何生成一个包含 res1 和 res2 的 blob,然后我可以使用 res1 和 res blob 生成一个 pdf 文件

DriveApp.getFoldersByName("a").next().createFile(blobs);

我尝试使用以下内容但失败了。

var blobs = res1.getBlob()+res2.getBlob

const url1 = "https://docs.google.com/spreadsheets/d/SHEETID/export?format=pdf&gid=788671078"
const url2= "https://docs.google.com/spreadsheets/d/SHEETID/export?format=pdf&gid=788671070"
var res1 = UrlFetchApp.fetch(url1,para); var res2 = UrlFetchApp.fetch(url2,para)

DriveApp.getFoldersByName("a").next().createFile(res1.getBlob());
DriveApp.getFoldersByName("a").next().createFile(res2.getBlob());

以上代码可以生成2个pdf文件。我想生成一个包含这 2 个 pdf 文件页面的 pdf。

【问题讨论】:

  • 在你的情况下,我认为不能直接实现合并这些 blob。比如你可以使用外部API的时候,我觉得是可以实现的。如果你不能使用外部API,我想考虑是否可以合并url1url2的Spreadsheets。你能提供这方面的详细信息吗?如果这不是您期望的方向,我深表歉意。
  • const url1 = "https://docs.google.com/spreadsheets/d/SHEETID/export?format=pdf&gid=788671078" const url2= "https://docs.google.com/spreadsheets/d/SHEETID/export?format=pdf&gid=788671070" var res1 = UrlFetchApp.fetch(url1,para); var res2 = UrlFetchApp.fetch(url2,para) DriveApp.getFoldersByName("a").next().createFile(res1.getBlob()); DriveApp.getFoldersByName("a").next().createFile(res2.getBlob());以上代码可以生成2个pdf文件。我想生成一个包含这 2 个 pdf 文件页面的 pdf。
  • 谢谢你的回复。从您的回复中,我提出了解决您的问题的解决方法。如果这对您的情况没有用,我深表歉意。顺便说一下,当你展示你的脚本时,请将它添加到你的问题中。这样,可读性就会高。

标签: google-apps-script google-drive-api urlfetch export-to-pdf


【解决方案1】:

问题和解决方法:

在你的情况下,我认为不能直接实现合并这些 blob。比如你可以使用外部API的时候,我觉得是可以实现的。如果你不能使用外部API,我想考虑是否可以合并url1和url2的Spreadsheets。通过这个,我在评论中询问了您当前的脚本。

根据您当前的脚本,我认为在这种情况下,2 张纸可以导出为一个 PDF 文件。在这个答案中,我想提出以下示例脚本。

示例脚本 1:

const url1 = "https://docs.google.com/spreadsheets/d/SHEETID/export?format=pdf&gid=788671078"const url2= "https://docs.google.com/spreadsheets/d/SHEETID/export?format=pdf&gid=788671070" 的脚本来看,SHEETID 似乎对于 2 张是一样的。在这种情况下,下面的脚本怎么样?此脚本仅显示您想要的 2 个工作表,电子表格导出为 PDF 文件。这样,PDF 文件仅包含 2 张。

function myFunction() {
  const gids = ["788671078", "788671070"]; // These are from your URLs.
  const spreadsheetId = "SHEETID"; // This is from your URLs.

  const sheets = SpreadsheetApp.openById(spreadsheetId).getSheets();
  sheets.forEach(s => {
    s[!gids.includes(s.getSheetId().toString()) ? "hideSheet" : "showSheet"]();
  });
  const url = `https://docs.google.com/spreadsheets/d/${spreadsheetId}/export?format=pdf`;
  const res = UrlFetchApp.fetch(url, {headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()}});
  DriveApp.getFoldersByName("a").next().createFile(res.getBlob());
  sheets.forEach(s =>  s.showSheet());
}

示例脚本 2:

在此示例脚本中,假设您的 2 张工作表放在不同的 2 个电子表格中。将 2 张工作表放入临时 Google 电子表格,并将电子表格导出为 PDF 文件。这样,PDF 文件仅包含 2 张。

function myFunction() {
  const gids = [{spreadsheetId: "SHEETID1", gid: "788671078"}, {spreadsheetId: "SHEETID2", gid: "788671070"}]; // These are from your URLs.

  const temp = SpreadsheetApp.create("temp");
  const ssId = temp.getId();
  gids.forEach(({spreadsheetId, gid}) => {
    const ss = SpreadsheetApp.openById(spreadsheetId)
    ss.getSheets().forEach(s => {
      if (s.getSheetId() == gid) {
        const ts = s.copyTo(ss);
        const r = ts.getDataRange();
        r.copyTo(r, {contentsOnly: true});
        ts.copyTo(temp);
        ss.deleteSheet(ts);
      }
    });
  });
  temp.deleteSheet(temp.getSheets()[0]);
  SpreadsheetApp.flush();
  const url = `https://docs.google.com/spreadsheets/d/${ssId}/export?format=pdf`;
  const res = UrlFetchApp.fetch(url, {headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()}});
  DriveApp.getFoldersByName("a").next().createFile(res.getBlob());
  DriveApp.getFileById(ssId).setTrashed(true);
}

参考:

【讨论】:

    【解决方案2】:

    这不能在本地完成。

    Blob object 是本机 Apps 脚本数据交换对象,无法按照您使用它的方式添加/连接 + operator

    因此,您将不得不使用完成合并的第三方 API,或者尝试重构为不同 JavaScript 环境编写的现有 PDF 合并库(不推荐)。

    有许多提供 PDF 合并服务的 API。我不能具体推荐任何一个,但 a Google search 给出了一些例子的许多结果(有些甚至有很好的记录!)。

    【讨论】:

      猜你喜欢
      • 2011-10-10
      • 1970-01-01
      • 2017-06-19
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-04
      • 1970-01-01
      相关资源
      最近更新 更多