簡體   English   中英

google-app-script 如何將所有 blob 合並為一個 blob

[英]google-app-script how to cocat all blob into one blob

var res1 = UrlFetchApp.fetch(url1,para);
var res2 = UrlFetchApp.fetch(url2,para)

上面的 url1&url2 是從谷歌表中導出 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文件。 我想生成一個 pdf 包括這 2 個 pdf 文件頁面。

問題和解決方法:

在您的情況下,我認為無法直接合並這些 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"腳本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);
}

參考:

這不能在本地完成。

Blob object是本機應用程序腳本數據交換 object 並且不能以您使用它的方式與+運算符添加/連接。

因此,您將不得不使用第三方 API 來完成合並,或者嘗試重構為不同的 JavaScript 環境編寫的現有 PDF 合並庫(不推薦)。

有許多 API 提供 PDF 合並服務。 我不能特別推薦任何一個,但谷歌搜索給出了一些示例的許多結果(有些甚至有據可查。)。

暫無
暫無

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

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