将Google表格另存为epub [英] Saving a Google Sheet as epub
问题描述
我有一个Google表格,其内容位于第1列.我正在寻找一个脚本,该脚本可以将第1行的Col 1保存为"epub",并将每行句子(作为一行)另存为新页面.
I have a Google Sheet with content in say Col 1. One sentence in each row. I am looking for a script which can save the Col 1 as 'epub' with each sentence (in row) as a new page.
推荐答案
我相信您的当前情况和您的目标如下.
I believe your current situation and your goal as follows.
- 在电子表格中,"A"列的每一行都有句子.
- 您要从"A"列的单元格中检索值.并将其转换为EPUB在您的Google云端硬盘中的文件.
- 您想使用Google Apps脚本实现这一目标.
在这种情况下,我想提出以下流程.
In this case, I would like to propose the following flow.
- 从列"A"中检索值.电子表格的内容.
- 将Google文档创建为临时文件.
- 将值复制到Google文档中.
- 将Google文档导出为
application/epub + zip
的EPUB,并将其另存为Google Drive中的文件. - 删除临时文件.
- Retrieve the values from the column "A" of the Spreadsheet.
- Create Google Document as the temporal file.
- Copy the values to Google Document.
- Export Google Document as EPUB of
application/epub+zip
and save it as a file on Google Drive. - Remove the temporal file.
当上述流程反映到脚本中时,它如下所示.
When above flow is reflected to the script, it becomes as follows.
请将以下脚本复制并粘贴到您要使用的Google Spreadsheet的脚本编辑器中.并且,请运行 myFunction
.这样,就从单元格"A1:A"中检索值.并使用每一行的值创建EPUB文件.
Please copy and paste the following script to the script editor of Google Spreadsheet you want to use. And, please run myFunction
. By this, the values are retrieved from the cells "A1:A" and create EPUB files using the values of each row.
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.参考 - In this sample script, the filename is
rowNumber${i + 1}
. So, the created filename is likerowNumber1.epub
,rowNumber2.epub
. If you want to change this, please modify above script. - The endpoint of
const url = "https://docs.google.com/feeds/download/documents/export/Export?exportFormat=epub&id=" + id;
is fromexportLinks
of the method of "Files: get" of Drive API. Ref -
在这种情况下,当电子表格中存在很多行时,处理时间可能超过了6分钟的最大执行时间.请注意这一点.如果处理时间超过了最大执行时间,请修改
startRow
和endRow
的值.
如果发生与Drive API相关的错误,请请在高级Google服务上启用Drive API .
If an error related to Drive API occurs, please enable Drive API at Advanced Google servicves.
如果要转换列"A"的值,请执行以下操作:作为一个EPUB文件,您还可以使用以下脚本.
If you want to convert the values of the column "A" as one EPUB file, you can also use the following script.
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);
}
这篇关于将Google表格另存为epub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!