首页 > 解决方案 > 将 Google 表格另存为 epub

问题描述

我有一个谷歌表格,内容是 Col 1。每行一个句子。我正在寻找一个脚本,它可以将 Col 1 保存为“epub”,每个句子(行中)作为一个新页面。

标签: google-apps-scriptgoogle-sheets

解决方案


我相信你现在的情况和你的目标如下。

  • 在您的电子表格中,“A”列的每一行中都有句子。
  • 您想从“A”列的单元格中检索一个值,并将其转换为 Google Drive 上的 EPUB 文件。
  • 您想使用 Google Apps 脚本实现此目的。

在这种情况下,我想提出以下流程。

  1. 从电子表格的“A”列中检索值。
  2. 创建 Google 文档作为临时文件。
  3. 将值复制到 Google 文档。
  4. 将 Google 文档导出为 EPUBapplication/epub+zip并将其另存为 Google Drive 上的文件。
  5. 删除临时文件。

当上述流程反映到脚本时,它变成如下。

示例脚本:

请将以下脚本复制并粘贴到您要使用的 Google 电子表格的脚本编辑器中。并且,请运行myFunction。这样,从单元格“A1:A”中检索值并使用每行的值创建 EPUB 文件。

function myFunction() {
  const sheetName = "Sheet1"; // Please set the sheet name.
  const folderId = "root"; // Please set the folder ID you want to export the EPUB files. In the case of "root", the files are created to the root folder.
  
  // 1. Retrieve the values from the column "A" of the Spreadsheet.
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  const startRow = 1;
  const endRow = sheet.getLastRow();
  const values = sheet.getRange(`A${startRow}:A${endRow}`).getDisplayValues();

  // 2. Create Google Document as the temporal file.
  const tempDoc = DocumentApp.create("temp");
  const id = tempDoc.getId();
  const url = "https://docs.google.com/feeds/download/documents/export/Export?exportFormat=epub&id=" + id;
  const params = {headers: {authorization: `Bearer ${ScriptApp.getOAuthToken()}`}};
  const folder = DriveApp.getFolderById(folderId || "root");

  const ids = values.map(([a], i) => {
    // 3. Copy the values to Google Document.
    const filename = `rowNumber${i + 1}`;
    const doc = DocumentApp.openById(id).setName(filename);
    doc.getBody().clear().appendParagraph(a);
    doc.saveAndClose();

    // 4. Export Google Document as EPUB of `application/epub+zip` and save it as a file on Google Drive.
    const blob = UrlFetchApp.fetch(url, params).getBlob().setName(`${filename}.epub`);
    return folder.createFile(blob).getId();
  });
  console.log(ids); // Here, you can see the file IDs of the created EPUB files at the log.

  // 5. Remove the temporal file.
  DriveApp.getFileById(id).setTrashed(true);
}
  • 在此示例脚本中,文件名为rowNumber${i + 1}. 因此,创建的文件名类似于rowNumber1.epub, rowNumber2.epub。如果你想改变这个,请修改上面的脚本。
  • 的端点const url = "https://docs.google.com/feeds/download/documents/export/Export?exportFormat=epub&id=" + id;来自exportLinksDrive API 的“Files:get”方法。参考

笔记:

  • 在这种情况下,当您的电子表格中存在大量行时,处理时间可能会超过 6 分钟的最大执行时间。请注意这一点。如果处理时间超过最大执行时间,请修改 和 的startRowendRow

  • 如果发生与 Drive API 相关的错误,请在 Advanced Google 服务中启用 Drive API

  • 如果要将“A”列的值转换为一个 EPUB 文件,也可以使用以下脚本。

      function myFunction2() {
        const sheetName = "Sheet1";
        const folderId = "root"; // Please set the folder ID you want to export the EPUB files. In the case of "root", the files are created to the root folder.
        const filename = `sampleFile`; // Please set the output filename.
    
        // 1. Retrieve the values from the column "A" of the Spreadsheet.
        const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
        const startRow = 1;
        const endRow = sheet.getLastRow();
        const values = sheet.getRange(`A${startRow}:A${endRow}`).getDisplayValues();
    
        // 2. Create Google Document as the temporal file.
        const tempDoc = DocumentApp.create(filename);
    
        // 3. Copy the values to Google Document.
        tempDoc.getBody().clear().appendParagraph(values.flat().join("\n"));
        tempDoc.saveAndClose();
        const id = tempDoc.getId();
    
        // 4. Export Google Document as EPUB of `application/epub+zip` and save it as a file on Google Drive.
        const url = "https://docs.google.com/feeds/download/documents/export/Export?exportFormat=epub&id=" + id;
        const params = {headers: {authorization: `Bearer ${ScriptApp.getOAuthToken()}`}};
        const folder = DriveApp.getFolderById(folderId || "root");
        const blob = UrlFetchApp.fetch(url, params).getBlob().setName(`${filename}.epub`);
        const createdFileId = folder.createFile(blob).getId();
        console.log(createdFileId); // Here, you can see the file ID of the created EPUB file at the log.
    
        // 5. Remove the temporal file.
        DriveApp.getFileById(id).setTrashed(true);
      }
    

参考:


推荐阅读