Skip to content

Instantly share code, notes, and snippets.

@barrieroberts
Last active April 2, 2021 15:14
Show Gist options
  • Save barrieroberts/a7c76d36e40fb95a06751a8fd3c84422 to your computer and use it in GitHub Desktop.
Save barrieroberts/a7c76d36e40fb95a06751a8fd3c84422 to your computer and use it in GitHub Desktop.
8-Get files by type
//Get any Google Docs in folder and add a paragraph to all of them
function example81() {
const parentFolder = DriveApp.getFolderById('ADD YOU FOLDER ID HERE');
const listOfDocs = parentFolder.getFilesByType(MimeType.GOOGLE_DOCS);
while (listOfDocs.hasNext()) {
var file = listOfDocs.next();
var docId = file.getId();
var doc = DocumentApp.openById(docId);
var body = doc.getBody();
body.appendParagraph("This paragraph was added automatically.");
}
}
//Delete files of a specific type that haven't been updated for over a week
function example82() {
const parentFolder = DriveApp.getFolderById('ADD YOU FOLDER ID HERE');
const listOfSsheets = parentFolder.getFilesByType(MimeType.GOOGLE_SHEETS);
while (listOfSsheets.hasNext()) {
var file = listOfSsheets.next();
if (new Date() - file.getLastUpdated() > 7 * 24 * 60 * 60 * 1000) {
file.setTrashed(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment