- You want to copy the 1st table and the 2nd and 3rd lists in the source Google Document to another Google Document.
- In your situation, you want to retrieve 2 lists after the 1st table. And you want to retrieve the paragraphs between the table and list.
- This is from your shared Document.
- You want to achieve this using Google Apps Script.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Issue and workaround:
Unfortunately, in the current stage, Document service cannot be used the glyph symbol of ✓
. By this, when the list with the glyph symbol of ✓
is copied by setting the original glyph symbol, the default symbol is used. In order to resolve this issue, I used the following flow.
- Copy the Google Document.
- Scan the range you want to copy from the copied Google Document.
- Delete the range except for the range you want to copy from the copied Google Document.
- Delete the inline objects from the copied Google Document.
By above flow, the 1st table and the 2nd and 3rd lists can be copied. And in order to achieve this, I used Google Docs API. Because there are several inline objects in your Document, and unfortunately, I couldn't find the method for deleting them with Document service. The inline objects can be deleted by Docs API, and the ranges can be deleted by one API call.
Sample script:
Before you run the script, please enable Google Docs API at Advanced Google services.
function myFunction() {
var sourcedocId = '###'; // Please set the source Google Document ID.
var filenameOfDestDocument = "sampleDestDocument"; // Please set the destination filename of Google Document.
var fileId = DriveApp.getFileById(sourcedocId).makeCopy(filenameOfDestDocument).getId();
var doc = Docs.Documents.get(fileId);
var content = doc.body.content;
var obj = {table: 1, list: 2};
var reqs = [];
for (var i = 0; i < content.length; i++) {
if ("table" in content[i]) {
reqs.push({deleteContentRange: {range: {startIndex: 1, endIndex: content[i].startIndex - 1}}});
obj.table--;
} else if (obj.table == 0 && obj.list > 0 && "paragraph" in content[i] && "bullet" in content[i].paragraph) {
while ("paragraph" in content[i] && "bullet" in content[i].paragraph) ++i;
obj.list--;
} else if (obj.table == 0 && obj.list == 0) {
reqs.push({deleteContentRange: {range: {startIndex: content[i].endIndex, endIndex: content[content.length - 1].endIndex - 1}}});
break;
} else if ("paragraph" in content[i] && "positionedObjectIds" in content[i].paragraph) {
Array.prototype.push.apply(reqs, content[i].paragraph.positionedObjectIds.map(function(e) {return {deletePositionedObject: {objectId: e}}}));
}
}
Docs.Documents.batchUpdate({requests: reqs.reverse()}, fileId);
}
Note:
- In this script, the destination Google Document is created to the same folder of the source Google Document.
- In this case, the script can be used for your shared Google Document. If you change the document, please modify the script.
References:
If I misunderstood your question and this was not the direction you want, I apologize.