Are there ways to make this script faster?

369 views
Skip to first unread message

VENKATESHA PRASAD KATTHAN

unread,
Feb 17, 2021, 10:25:11 AM2/17/21
to Google Apps Script Community
Enter code here...
function masterFunction (){
  copySubDocOne();
  copySubDocTwo();
}

function copySubDocOne() {
  var mainDocID = ' '//$Test Main
  var mainDocTarget = DocumentApp.openById(mainDocID);

  var subOne = ' '
  var sourceDocOne = DocumentApp.openById(subOne).getBody();
  //var mainDocTarget = DocumentApp.create('CopyOf'+DocumentApp.getActiveDocument().getName());
  var mainDocTarget = DocumentApp.openById(mainDocID);
  var totalElements = sourceDocOne.getNumChildren();

  for (var j = 0j < totalElements; ++j) {
    var body = mainDocTarget.getBody()
    var element = sourceDocOne.getChild(j).copy();
    var type = element.getType();
    //Paragraphs
    if (type == DocumentApp.ElementType.PARAGRAPH) {
      body.appendParagraph(element);
      mainDocTarget.saveAndClose();
      var mainDocTarget = DocumentApp.openById(mainDocID);

    }
    //Table
    else if (type == DocumentApp.ElementType.TABLE) {
      body.appendTable(element);
      mainDocTarget.saveAndClose();
      var mainDocTarget = DocumentApp.openById(mainDocID);
    }

    //Bulets Working without Bullets
    else if (type == DocumentApp.ElementType.LIST_ITEM) {
      body.appendListItem(element).setGlyphType(DocumentApp.GlyphType.BULLET);
      mainDocTarget.saveAndClose();
      var mainDocTarget = DocumentApp.openById(mainDocID);
    }
    //Pagebreaks
    else if (type == DocumentApp.ElementType.PAGE_BREAK) {
      body.appendListItem(element);
      mainDocTarget.saveAndClose();
      var mainDocTarget = DocumentApp.openById(mainDocID);
    }
    //Horizontal Rule
    else if (type == DocumentApp.ElementType.HORIZONTAL_RULE) {
      body.appendListItem(element);
      mainDocTarget.saveAndClose();
      var mainDocTarget = DocumentApp.openById(mainDocID);
    }
    //Inline Drawing
    else if (type == DocumentApp.ElementType.INLINE_DRAWING) {
      body.appendListItem(element);
      mainDocTarget.saveAndClose();
      var mainDocTarget = DocumentApp.openById(mainDocID);
    }
    //Inline Image
    else if (type == DocumentApp.ElementType.INLINE_IMAGE) {
      body.appendListItem(element);
      mainDocTarget.saveAndClose();
      var mainDocTarget = DocumentApp.openById(mainDocID);
    }
    //Unsupported
   /* else if (type == DocumentApp.ElementType.UNSUPPORTED) {
      body.appendListItem(element);
      mainDocTarget.saveAndClose();
      var mainDocTarget = DocumentApp.openById(mainDocID);
    }*/

  }
  mainDocTarget.saveAndClose();
}


function copySubDocTwo() {
  var mainDocID = ' '//$Test Main
  var mainDocTarget = DocumentApp.openById(mainDocID);


  var subTwo = ' '    //---Here---
  var sourceDocTwo = DocumentApp.openById(subTwo).getBody();     //---Here---Twice--
  var totalElements = sourceDocTwo.getNumChildren();             //---Here---



  for (var j = 0j < totalElements; ++j) {
    var body = mainDocTarget.getBody()
    var element = sourceDocTwo.getChild(j).copy();               //---Here---
    var type = element.getType();
    //Paragraphs
    if (type == DocumentApp.ElementType.PARAGRAPH) {
      body.appendParagraph(element);
      mainDocTarget.saveAndClose();
      var mainDocTarget = DocumentApp.openById(mainDocID);

    }
    //Table
    else if (type == DocumentApp.ElementType.TABLE) {
      body.appendTable(element);
      mainDocTarget.saveAndClose();
      var mainDocTarget = DocumentApp.openById(mainDocID);
    }
    //Bullets

    //Bulets Working without Bullets
    else if (type == DocumentApp.ElementType.LIST_ITEM) {
      body.appendListItem(element).setGlyphType(DocumentApp.GlyphType.BULLET);
      mainDocTarget.saveAndClose();
      var mainDocTarget = DocumentApp.openById(mainDocID);
    }
    //Pagebreaks
    else if (type == DocumentApp.ElementType.PAGE_BREAK) {
      body.appendListItem(element);
      mainDocTarget.saveAndClose();
      var mainDocTarget = DocumentApp.openById(mainDocID);
    }
    //Horizontal Rule
    else if (type == DocumentApp.ElementType.HORIZONTAL_RULE) {
      body.appendListItem(element);
      mainDocTarget.saveAndClose();
      var mainDocTarget = DocumentApp.openById(mainDocID);
    }
    //Inline Drawing
    else if (type == DocumentApp.ElementType.INLINE_DRAWING) {
      body.appendListItem(element);
      mainDocTarget.saveAndClose();
      var mainDocTarget = DocumentApp.openById(mainDocID);
    }
    //Inline Image
    else if (type == DocumentApp.ElementType.INLINE_IMAGE) {
      body.appendListItem(element);
      mainDocTarget.saveAndClose();
      var mainDocTarget = DocumentApp.openById(mainDocID);
    }
    //Unsupported
    /*else if (type == DocumentApp.ElementType.UNSUPPORTED) {
      body.append(element);
      mainDocTarget.saveAndClose();
      var mainDocTarget = DocumentApp.openById(mainDocID);
    }*/

  }
  mainDocTarget.saveAndClose();
}


Steven Bazyl

unread,
Feb 17, 2021, 10:52:52 AM2/17/21
to google-apps-sc...@googlegroups.com
If you're literally just copying the documents in their entirety, use DriveApp to copy the file. Way easier and faster than trying to copy individual elements.


This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email by mistake, please notify Emilio Aguinaldo College by returning the original message to sender and immediately delete this email from your system. If you are not the intended recipient, you are hereby notified that any dissemination, distribution, copying or taking any action in reliance on the contents of this information is strictly prohibited.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/0199ed9b-f47f-4770-9673-a10e45562027o%40googlegroups.com.

vkpkatt...@eac.edu.ph

unread,
Feb 17, 2021, 10:58:24 AM2/17/21
to Google Apps Script Community
Hey, thanks for the reply. I need to merge data from 25 documents into one document every week. I just made two at the moment. I’m looking at somewhere around 200+300 pages every week. 
Does DriveApp have this functionality? 

Steven Bazyl

unread,
Feb 17, 2021, 11:27:25 AM2/17/21
to google-apps-sc...@googlegroups.com
No. But in that case, remove these lines from inside the loop as it's expensive to save and re-open the doc after copying each element:

 mainDocTarget.saveAndClose();
 var mainDocTarget = DocumentApp.openById(mainDocID);

VENKATESHA PRASAD KATTHAN

unread,
Feb 17, 2021, 11:30:44 AM2/17/21
to google-apps-sc...@googlegroups.com
It was at the end at first, then I had an error saying: Document couldn’t be saved because it’s big (paraphrased). So I thought I would save them every time. Is there any place in the loop I could place them in? To maximise efficiency?

Jonathan Butler

unread,
Feb 17, 2021, 7:12:06 PM2/17/21
to google-apps-sc...@googlegroups.com
Do the pages need to be in order or just need to be added to the document? Also, how long does it take currently?

vkpkatt...@eac.edu.ph

unread,
Feb 17, 2021, 8:18:24 PM2/17/21
to Google Apps Script Community
I managed to save them in an array. Now it’s fast like fast fast. It used to take 20 mins for my test 80 pages. Now it’s done in 40 seconds. 
the new code looks something like this 

function CopySubDocuments() {
  var mainDoc = '   '; //$Test Main
  var mainDocTarget = DocumentApp.openById(mainDoc);


  var subDocs = [30];
  subDocs[1] = '   ';
  subDocs[2] = '   ';
  //var sourceDocOne = DocumentApp.openById(subDocs[1]).getBody();
  //var sourceDocTwo = DocumentApp.openById(subDocs[2]).getBody();



  for (documentNumber = 1; documentNumber < 3; ++documentNumber) {
    sourceDocOne = DocumentApp.openById(subDocs[documentNumber]).getBody();

    var mainDocTarget = DocumentApp.openById(mainDoc);
    var totalElements = sourceDocOne.getNumChildren();

    for (var j = 0; j < totalElements; ++j) {
      var body = mainDocTarget.getBody();

      var bodyArray = [2];
      bodyArray[1] = sourceDocOne.getChild(j).copy();
      bodyArray[2] = bodyArray[1].getType();
      //Paragraphs
      if (bodyArray[2] == DocumentApp.ElementType.PARAGRAPH) {
        body.appendParagraph(bodyArray[1]);
      }
      //Table
      else if (bodyArray[2] == DocumentApp.ElementType.TABLE) {
        body.appendTable(bodyArray[1]);

      }
      //Bulets Working without Bullets
      else if (bodyArray[2] == DocumentApp.ElementType.LIST_ITEM) {
        body.appendListItem(bodyArray[1]).setGlyphType(DocumentApp.GlyphType.BULLET);

      }
      //Pagebreaks
      else if (bodyArray[2] == DocumentApp.ElementType.PAGE_BREAK) {
        body.appendListItem(bodyArray[1]);

      }
      //Horizontal Rule
      else if (bodyArray[2] == DocumentApp.ElementType.HORIZONTAL_RULE) {
        body.appendListItem(bodyArray[1]);

      }
      //Inline Drawing
      else if (bodyArray[1] == DocumentApp.ElementType.INLINE_DRAWING) {
        body.appendListItem(bodyArray[2]);

      }
      //Inline Image
      else if (bodyArray[1] == DocumentApp.ElementType.INLINE_IMAGE) {
        body.appendListItem(bodyArray[2]);

      }

    }
    mainDocTarget.saveAndClose();
    var mainDocTarget = DocumentApp.openById(mainDoc);
  }
  mainDocTarget.saveAndClose();
}


im sorry about the formatting coudn't do much better. 

Can someone please check if its fool proo fand bug free. 

Thank you for spending your time. 
Reply all
Reply to author
Forward
0 new messages