[Google Apps Script] Replace the body text of a Google Doc

Created on November 30, 2026

You can open a Google Doc with DocumentApp.openByUrl() and call setText() on its body. This replaces the current body text. Use the URL of a document you can edit.

function main() {
  var doc = DocumentApp.openByUrl('https://example.com/some_document_url');
  var body = doc.getBody();
  body.setText('hello world');
}
Google Apps Script