Create a gist now

Instantly share code, notes, and snippets.

@rheajt /Code.gs
Last active May 24, 2017

What would you like to do?
copy titles into all documents in a folder
function copyTitles() {
//Replace this variable with the folder id
//you can get the id from the URL
var folderId = 'REPLACE_WITH_FOLDER_ID';
var folder = DriveApp.getFolderById(folderId);
var files = folder.getFilesByType(MimeType.GOOGLE_DOCS);
//this will iterate over every single DOCUMENT in that folder
//it will ignore spreadsheets, forms, and other files
//you can modify the mimetype above to change that behavior
while(files.hasNext()) {
var file = files.next();
var fileId = file.getId();
//this script takes the file name and inserts it into the very
//top of the document.
var fileName = file.getName();
DocumentApp.openById(fileId).getBody()
.insertParagraph(0, fileName)
.setAlignment(DocumentApp.HorizontalAlignment.CENTER)
.setHeading(DocumentApp.ParagraphHeading.HEADING1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment