The script starts by getting the ID of the file to copy ('getFileById'), then the destination folder ID of where the new file will be copied ('getFolderById'). A copy is then made using these parameters, along with a new file name of new doc (this can be altered as required). During the copy it grabs the file Url ('getUrl') so that 'DocumentApp' can then open it and get the body to then change a tag for <<Name>> and loop through adding 5 paragraph lines.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function copyDoc_Paragraphs() { | |
// document ID of the file to be copied | |
var docId = 'insert your file ID'; | |
// instruct DriveApp to get this document | |
var templateDoc = DriveApp.getFileById(docId); | |
// folder ID of where the copied file is to be saved | |
var folderId = 'insert your folder ID'; | |
// instruct DriveApp to get this folder | |
var destinationFolder = DriveApp.getFolderById(folderId); | |
// copy the file to the destination and get the Url | |
var newDocUrl = templateDoc.makeCopy('new doc', destinationFolder).getUrl(); | |
// open the file by the Url | |
var newDoc = DocumentApp.openByUrl(newDocUrl); | |
// get the body of the document | |
var docBody = newDoc.getBody(); | |
// replace the tag with 'Eric' | |
docBody.replaceText('<<Name>>', 'Eric'); | |
// add 5 paragraph lines | |
for (var i=1; i<6; i++) { | |
docBody.appendParagraph('this is paragraph ' + i); | |
} | |
} |
No comments:
Post a Comment