alexdobrenko
alexdobrenko

Reputation: 11

Google Scripts - Find and Replace Function that searches multiple documents

I'm new to Google Scripts and am looking for a good place to start and educate myself on being able to write a script that accomplishes the following:

It scans a number of different google docs and does a find and replace, essentially a mail merge, as instructed by the code. So basically each doc would have a [REPLACE TEXT HERE] already in it and I'd tell the script that I'd like each [REPLACE TEXT HERE] changed to [WORD A] for a number of different documents.

I understand this is may be a basic request, so if there's a place to point me towards that walks me through google script basics that would include this, that'd be great - my initial research did not have anything that honed in exactly on this specific need.

Thanks for your help everyone!

Upvotes: 1

Views: 5377

Answers (1)

Jonathan Crosmer
Jonathan Crosmer

Reputation: 786

Here's how I did it. (I learned how to use the API from the documentation.)

function myFunction() {
  var files = DriveApp.getFiles();   // Note: this gets *every* file in your Google Drive
  while (files.hasNext()) {
    var file = files.next();
    Logger.log(file.getName());
    var doc = DocumentApp.openById(file.getId());
    doc.replaceText("My search string or regex", "My replacement string");
  }
  Logger.log("Done")
}

Upvotes: 1

Related Questions