google-apps-script - 将 Google 表格另存为 epub
问题描述
我有一个谷歌表格,内容是 Col 1。每行一个句子。我正在寻找一个脚本,它可以将 Col 1 保存为“epub”,每个句子(行中)作为一个新页面。
解决方案
我相信你现在的情况和你的目标如下。
- 在您的电子表格中,“A”列的每一行中都有句子。
- 您想从“A”列的单元格中检索一个值,并将其转换为 Google Drive 上的 EPUB 文件。
- 您想使用 Google Apps 脚本实现此目的。
在这种情况下,我想提出以下流程。
- 从电子表格的“A”列中检索值。
- 创建 Google 文档作为临时文件。
- 将值复制到 Google 文档。
- 将 Google 文档导出为 EPUB
application/epub+zip
并将其另存为 Google Drive 上的文件。 - 删除临时文件。
当上述流程反映到脚本时,它变成如下。
示例脚本:
请将以下脚本复制并粘贴到您要使用的 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;
来自exportLinks
Drive API 的“Files:get”方法。参考
笔记:
在这种情况下,当您的电子表格中存在大量行时,处理时间可能会超过 6 分钟的最大执行时间。请注意这一点。如果处理时间超过最大执行时间,请修改 和 的
startRow
值endRow
。如果发生与 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); }
参考:
推荐阅读
- python - 如何以最少的处理器消耗杀死进程
- scala - 带有下划线的 Scala 字符串插值
- c++ - 尝试添加数组元素给出零
- node.js - 在 vs 代码中调试用 typescript 节点编写的 jasmine 测试
- javascript - 如何在悬停时获取 img src 值,使用纯 JavaScript 即时编辑和返回它?
- javascript - 单击按钮时,如何添加在画布上水平移动的随机大小的黑色块?
- iterator - 是否可以有条件地应用迭代器转换?
- php - 如何将多个 where 子句变量传递给 bind_param?
- octobercms - 保存用户个人资料详细信息,十月 CMS
- firebase - 是否可以从 linux shell 脚本从 Cloud Storage 检索文档?