Google Docs: Document; Replace All Text

Replaces all instances of text matching the specified string with replace text. The search is case sensitive. The number of replacements is output as a log. In many cases, the automated step “Google Drive: File; Copy” is placed upstream.

2019-09-25 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/google-docs-document-replace-all-text/

Configs
  • A: Select OAuth2 Config Name (at [OAuth 2.0 Setting]) *
  • B: Set Document-ID of Docs File (44 chars in File URI) * #{EL}
  • C: Set SUBSTRING to Search (e.g. “XXXX-XX-XX”) * #{EL}
  • D: Set SUBSTRING to Replace #{EL}
Script
// (c) 2019, Questetra, Inc. (the MIT License)

//// == OAuth2 Setting example ==
// Authorization Endpoint URL:
//  "https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force"
// Token Endpoint URL:
//  "https://accounts.google.com/o/oauth2/token"
// Scope:
//  "https://www.googleapis.com/auth/documents"
//  If used with 'Google Drive: File; Copy', full permission may be appropriate.
//  "https://www.googleapis.com/auth/drive"
// Client ID:
//  ( from https://console.developers.google.com/ )
// Consumer Secret:
//  ( from https://console.developers.google.com/ )
//  *Redirect URLs: "https://s.questetra.net/oauth2callback"


//////// START "main()" ////////
main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const oauth2       = configs.get( "conf_OAuth2"  ) + "";
const documentId   = configs.get( "conf_DocumentId") + "";
const substring    = configs.get( "conf_Substring" ) + "";
const newSubstring = configs.get( "conf_NewSubstring" ) + "";
// 'java.lang.String' (String Obj) to javascript primitive 'string'

engine.log( " AutomatedTask Config: Document ID: " + documentId );
engine.log( " AutomatedTask Config: Substring: " + substring );
engine.log( " AutomatedTask Config: NewSubstring: " + newSubstring );


//// == Data Retrieving / ワークフローデータの参照 ==
// (nothing)


//// == Calculating / 演算 ==
/// obtain OAuth2 Access Token
const token   = httpClient.getOAuth2Token( oauth2 );

/// post ReplaceAllTextRequest
let requestObj = {};
    requestObj.requests = [];
    requestObj.requests[0] = {};
    requestObj.requests[0].replaceAllText = {};
    requestObj.requests[0].replaceAllText.replaceText = newSubstring;
    requestObj.requests[0].replaceAllText.containsText = {};
    requestObj.requests[0].replaceAllText.containsText.text = substring;
    requestObj.requests[0].replaceAllText.containsText.matchCase = true;
    // if "false" : the search is case insensitive.
let apiRequest = httpClient.begin(); // HttpRequestWrapper
apiRequest = apiRequest.bearer( token );
apiRequest = apiRequest.body( JSON.stringify( requestObj ), "application/json" );
const apiUri = "https://docs.googleapis.com/v1/documents/" +
                documentId + ":batchUpdate";
engine.log( " AutomatedTask Trying: POST " + apiUri );
const response = apiRequest.post( apiUri );
const responseCode = response.getStatusCode() + "";
engine.log( " AutomatedTask ApiResponse: Status " + responseCode );
if( responseCode !== "200"){
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
         responseCode + "\n" + response.getResponseAsString() + "\n" );
}
const responseStr = response.getResponseAsString() + "";
const responseObj = JSON.parse( responseStr );
if( responseObj.replies[0].replaceAllText.occurrencesChanged == undefined ){
  engine.log( " AutomatedTask ApiResponse: Occurrences Changed 0" );
}else{
  engine.log( " AutomatedTask ApiResponse: Occurrences Changed " +
             responseObj.replies[0].replaceAllText.occurrencesChanged );
}


//// == Data Updating / ワークフローデータへの代入 ==
// (nothing)

} //////// END "main()" ////////

Download

Capture

Notes

  1. For example, replace all “XXXX-XX-XX” existing in Contract Template with “2019-09-25”.
  2. In many cases, the automated step “Google Drive: File; Copy” is placed upstream.
  3. If you want to set multiple replacement patterns at once, use “Replace All Text in Bulk”.
  4. [Google Blog] Manage workflows and automate processes with the new Google Docs API (2019-02-11)
  5. [Google Blog] Process paperwork pronto with the new Google Docs API

See also

1 thought on “Google Docs: Document; Replace All Text”

  1. Pingback: String, Replace All – Questetra Support

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: