从 Google 电子表格中的链接合并 Google 文档,仅选择其中的一部分
Merging Google Docs from links in a Google Spreadsheet, selecting only some of them
所以,我有一个 Google 电子表格,其中我为每个条目(使用模板)创建了一个 Google 文档,并在每一行的末尾添加了 link 到相应的文件。到目前为止,一切都很好。问题是,现在我希望能够将多个文档合并为一个,但我似乎无法管理,因为我希望能够导出它们的不同组合。在我找到的所有示例中(比如 this one) you need to enter the DocumentIDs or merge all the documents in a folder 或电子表格..我想要一些东西,我可以在我想要组合的条目的第一列(我的状态列)中写上“OK”,然后使用脚本来说
”如果状态列显示 'OK',则从 DocID 列检索 ID 并将其与所有其他也显示 'OK' 的 DocID 组合,无论他们有多少人。
到目前为止,我设法做到这一点的唯一方法是手动输入 DocID:
function mergeGoogleDocs() {
var docIDs = ['docID_1','docID_2','docID_3','docID_4'];
var baseDoc = DocumentApp.openById(docIDs[0]);
var body = baseDoc.getActiveSection();
for (var i = 1; i < docIDs.length; ++i ) {
var otherBody = DocumentApp.openById(docIDs[i]).getActiveSection();
var totalElements = otherBody.getNumChildren();
for( var j = 0; j < totalElements; ++j ) {
var element = otherBody.getChild(j).copy();
var type = element.getType();
if( type == DocumentApp.ElementType.PARAGRAPH )
body.appendParagraph(element);
else if( type == DocumentApp.ElementType.TABLE )
body.appendTable(element);
else if( type == DocumentApp.ElementType.LIST_ITEM )
body.appendListItem(element);
}
}
}
但是如果我想合并 4 或 5 个以上的文件,这很乏味,我必须手动去获取 table 中的信息,而不是过滤我的数据,在状态栏中写 OK 然后让Google 脚本为我合并了所有文件。
有人知道怎么做吗?
谢谢!
解决方案:
在合并之前,首先使用 Sheets API 使用此代码从电子表格填充您的 docID
数组。这假设您在 A 列中有 OK 状态列,在 B 列中有 ID。
示例代码:
function mergeGoogleDocs() {
// Populate document IDs
var docIDs = [];
var ss = SpreadsheetApp.getActiveSheet();
var range = ss.getRange(1,1,ss.getLastRow(),2).getValues();
for (j = 0; j < range.length; j++) {
if (range[j][0] == 'OK') {
docIDs.push(range[j][1]);
}
}
// Merge documents
var baseDoc = DocumentApp.openById(docIDs[0]);
var body = baseDoc.getActiveSection();
for (var i = 1; i < docIDs.length; ++i ) {
var otherBody = DocumentApp.openById(docIDs[i]).getActiveSection();
var totalElements = otherBody.getNumChildren();
for( var j = 0; j < totalElements; ++j ) {
var element = otherBody.getChild(j).copy();
var type = element.getType();
if( type == DocumentApp.ElementType.PARAGRAPH )
body.appendParagraph(element);
else if( type == DocumentApp.ElementType.TABLE )
body.appendTable(element);
else if( type == DocumentApp.ElementType.LIST_ITEM )
body.appendListItem(element);
}
}
}
示例数据:
参考文献:
所以,我有一个 Google 电子表格,其中我为每个条目(使用模板)创建了一个 Google 文档,并在每一行的末尾添加了 link 到相应的文件。到目前为止,一切都很好。问题是,现在我希望能够将多个文档合并为一个,但我似乎无法管理,因为我希望能够导出它们的不同组合。在我找到的所有示例中(比如 this one) you need to enter the DocumentIDs or merge all the documents in a folder 或电子表格..我想要一些东西,我可以在我想要组合的条目的第一列(我的状态列)中写上“OK”,然后使用脚本来说
”如果状态列显示 'OK',则从 DocID 列检索 ID 并将其与所有其他也显示 'OK' 的 DocID 组合,无论他们有多少人。
到目前为止,我设法做到这一点的唯一方法是手动输入 DocID:
function mergeGoogleDocs() {
var docIDs = ['docID_1','docID_2','docID_3','docID_4'];
var baseDoc = DocumentApp.openById(docIDs[0]);
var body = baseDoc.getActiveSection();
for (var i = 1; i < docIDs.length; ++i ) {
var otherBody = DocumentApp.openById(docIDs[i]).getActiveSection();
var totalElements = otherBody.getNumChildren();
for( var j = 0; j < totalElements; ++j ) {
var element = otherBody.getChild(j).copy();
var type = element.getType();
if( type == DocumentApp.ElementType.PARAGRAPH )
body.appendParagraph(element);
else if( type == DocumentApp.ElementType.TABLE )
body.appendTable(element);
else if( type == DocumentApp.ElementType.LIST_ITEM )
body.appendListItem(element);
}
}
}
但是如果我想合并 4 或 5 个以上的文件,这很乏味,我必须手动去获取 table 中的信息,而不是过滤我的数据,在状态栏中写 OK 然后让Google 脚本为我合并了所有文件。
有人知道怎么做吗? 谢谢!
解决方案:
在合并之前,首先使用 Sheets API 使用此代码从电子表格填充您的 docID
数组。这假设您在 A 列中有 OK 状态列,在 B 列中有 ID。
示例代码:
function mergeGoogleDocs() {
// Populate document IDs
var docIDs = [];
var ss = SpreadsheetApp.getActiveSheet();
var range = ss.getRange(1,1,ss.getLastRow(),2).getValues();
for (j = 0; j < range.length; j++) {
if (range[j][0] == 'OK') {
docIDs.push(range[j][1]);
}
}
// Merge documents
var baseDoc = DocumentApp.openById(docIDs[0]);
var body = baseDoc.getActiveSection();
for (var i = 1; i < docIDs.length; ++i ) {
var otherBody = DocumentApp.openById(docIDs[i]).getActiveSection();
var totalElements = otherBody.getNumChildren();
for( var j = 0; j < totalElements; ++j ) {
var element = otherBody.getChild(j).copy();
var type = element.getType();
if( type == DocumentApp.ElementType.PARAGRAPH )
body.appendParagraph(element);
else if( type == DocumentApp.ElementType.TABLE )
body.appendTable(element);
else if( type == DocumentApp.ElementType.LIST_ITEM )
body.appendListItem(element);
}
}
}
示例数据:
参考文献: