Reputation: 1625
My code currently searches for a predefined start tag in google document, and locates a corresponding ending tag. The position of the tags is retained in a variable.
Next the code runs through and copies all of the elements between the tags into a second google document. This works as expected.
However, the data held in the source document has grown to be 100s of pages. Therefore the script searching for the tags takes a few more seconds than expected. This increases the time it takes to find the tags and copy the elements between them.
My solution to this is to have a master script that picks up the elements once and stores them in something like Firebase or Firestore. Then I can access the elements directly from the DB. I have tried several approaches to store Google document elements.
This is how I get the elements.
var element = sourceDoc.getChild(j).copy(); // Gets Paragraph etc.
My attempts include.
Has anybody tried saving Google document elements for use a later date?
Upvotes: 1
Views: 59
Reputation:
It seems that Google Document elements don't have a representation that can be used outside of Apps Script environment. I suggest using other, smaller, documents to store the elements you'll need later. A script can create documents as needed (up to 250 per day) and access them by their IDs later. The IDs are strings; they can be associated with some keys according to which you located those elements, and this information can be JSON stringified and stored in Script Properties.
Since there is no appendElement
method, it seems one has to identify elements by type first, and then append to the target (and not every type is supported by append methods).
function storeElements() {
var elements = ... // your array of elements
var storage = DocumentApp.create("storage"); // if new is needed
var body = storage.getBody();
for (var i in elements) {
switch (selected[i].getType()) {
case DocumentApp.ElementType.INLINE_IMAGE:
body.appendImage(elements[i]); break;
case DocumentApp.ElementType.LIST_ITEM:
body.appendListItem(elements[i]); break;
case DocumentApp.ElementType.PARAGRAPH:
body.appendParagraph(elements[i]); break;
case DocumentApp.ElementType.TABLE:
body.appendTable(elements[i]); break;
case DocumentApp.ElementType.HORIZONTAL_RULE:
body.appendHorizontalRule(); break;
case DocumentApp.ElementType.PAGE_BREAK:
body.appendPageBreak(); break;
}
}
// store storage.getId(); in ScriptProperties
}
Upvotes: 0