Skip to content

Instantly share code, notes, and snippets.

@coffeejunk
Created November 28, 2015 18:09
Show Gist options
  • Save coffeejunk/7be7e88f423c7a820b09 to your computer and use it in GitHub Desktop.
Save coffeejunk/7be7e88f423c7a820b09 to your computer and use it in GitHub Desktop.
// There are two ways to run this script
//
// 1. Replace all Smart Quotes in all Docs that are inside of a folder (and all subfolders)
// 2. Replace all Smart Quotes in one particular file
//
// Choose the which one you want to use and then change the value of the appropriate var, then select the method from the menu and press Run.
// Replace Smart Quotes with Dumb Quotes for every Document that is inside of the given folder
function findAndReplaceAll() {
var folder_name = "OMG-PLEASE-CHANGE-ME"
var f = DriveApp.getFoldersByName(folder_name);
var bp_folder = f.next();
traverseFolder(bp_folder);
}
function findAndReplaceOne() {
var file_name = "OMG PLEASE CHANGE ME OH PLEAAAASEEEE";
f = DriveApp.getFilesByName(file_name);
if (f.hasNext()) {
file = f.next();
quoteReplace(file)
}
}
// This function does the actual find and replace
function quoteReplace(file) {
Logger.log(file);
var doc = DocumentApp.openById(file.getId());
var body = doc.getBody();
doc.replaceText("‘", "'");
doc.replaceText("’", "'");
doc.replaceText("“", "\"");
doc.replaceText("”", "\"");
doc.saveAndClose();
}
// This functions traverses all folders recursively
function traverseFolder(folderObj) {
traverseFiles(folderObj);
var subs = folderObj.getFolders();
while (subs.hasNext()) {
traverseFolder(subs.next());
}
}
// This function traverses all files (GOOGLE DOCS) in a given folder
function traverseFiles(folder) {
var doc, documents = folder.getFilesByType(MimeType.GOOGLE_DOCS);
while (documents.hasNext()) {
var file = documents.next();
Logger.log(file.getName());
quoteReplace(file);
}
}
@Nthole
Copy link

Nthole commented Nov 28, 2015

Thats deep

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment