0

I'm interested in generating a Google Document with a particular Apps Script from my library. The way I have been creating the Documents has been through basically, Jeff Everhart's code as seen here.

Is it possible that the code be changed as to have the new Google Docs have an embedded Apps Script right from the get go?

My reduced code:

  function createNewGoogleDocs() {
      const googleDocTemplate = DriveApp.getFileById('The template ID goes here');
      const destinationFolder = DriveApp.getFolderById('The destination folder ID goes here');

      const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
      const rows = sheet.getDataRange().getValues();
  
  
    rows.forEach(function(row, index) {
      if (index === 0 ) return;
      if(row[rows[0].length - 1]) return;


      const copy = googleDocTemplate.makeCopy(`First Column Name (${row[0]})`, destinationFolder);
      const doc = DocumentApp.openById(copy.getId());
    
      // Hopefully here we can have the addition of the the Apps Script on the new Docs

      doc.saveAndClose();
      const url = doc.getUrl();
      sheet.getRange(index+1, rows[0].length).setValue(url);


    })

  }
4
  • I have to apologize for my poor English skill. I cannot understand new Google Docs have an embedded Apps Script. Can I ask you about the detail of it? Commented Mar 8, 2022 at 2:42
  • 2
    make a copy of a google doc that has the script Commented Mar 8, 2022 at 3:11
  • Hello, @Tanaike. Thanks for commenting early. By "to have the new Google Docs have an embedded Apps Script", I mean that I want new Google Docs to have an Apps Script code already in place. Down below, Cooper already answered, but I appreaciate you came to answer early. Commented Mar 8, 2022 at 4:04
  • 1
    @Cooper, yes! I already tried it out and works as I wanted. I really thank you for your help. Commented Mar 8, 2022 at 4:05

1 Answer 1

1

If your goal is to create an Apps Script project bound to a Doc, you have to create it from that particular document. As it is said on the docs:

A script is bound to a Google Sheets, Docs, Slides, or Forms file if it was created from that document rather than as a standalone script. The file that a bound script is attached to is called a "container." Bound scripts generally behave like standalone scripts except that they do not appear in Google Drive, they cannot be detached from the file they are bound to, and they gain a few special privileges over the parent file.

If your end goal is to create a Doc with an embedded Apps Script from a different Apps Script project, you would have to first create that Doc/script as a template and then use makeCopy to create a copy of the Doc/script.

I am documenting this answer to help other people on this community, please feel free to leave a comment in your need further assistance.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.