Google Docs: Document, Replace All
Replaces all instances of text matching the FindWhat string with the ReplaceWith string in a case-sensitive way. It is also possible to replace multiple replacement patterns at once. If you are using a template file, you will often need to duplicate and replace the file in the upstream step.
Configs
- U: Select HTTP_Authz Setting *
- A1: Set FILE-ID in Drive *#{EL}
- B1: Set Substring for FindWhat#{EL}
- C1: Set Substring for ReplaceWith#{EL}
- B2: Set Substring for FindWhat#{EL}
- C2: Set Substring for ReplaceWith#{EL}
- B3: Set Substring for FindWhat#{EL}
- C3: Set Substring for ReplaceWith#{EL}
- B4: Set Substring for FindWhat#{EL}
- C4: Set Substring for ReplaceWith#{EL}
- B5: Set Substring for FindWhat#{EL}
- C5: Set Substring for ReplaceWith#{EL}
- B6: Set Substring for FindWhat#{EL}
- C6: Set Substring for ReplaceWith#{EL}
- B7: Set Substring for FindWhat#{EL}
- C7: Set Substring for ReplaceWith#{EL}
- B8: Set Substring for FindWhat#{EL}
- C8: Set Substring for ReplaceWith#{EL}
- B9: Set Substring for FindWhat#{EL}
- C9: Set Substring for ReplaceWith#{EL}
Script (click to open)
// GraalJS Script (engine type: 2)
//////// START "main()" /////////////////////////////////////////////////////////////////
main();
function main(){
//// == Config Retrieving / 工程コンフィグの参照 ==
const strAuthzSetting = configs.get ( "AuthzConfU" ); /// REQUIRED
engine.log( " AutomatedTask Config: Authz Setting: " + strAuthzSetting );
const strInputfileId = configs.get ( "StrConfA1" ); /// REQUIRED
if( strInputfileId === "" ){
throw new Error( "\n AutomatedTask ConfigError:" +
" Config {A1: FileID} is empty \n" );
}
const strFindWhat1 = configs.get ( "StrConfB1" ); // NotRequired
const strReplaceWith1 = configs.get ( "StrConfC1" ); // NotRequired
const strFindWhat2 = configs.get ( "StrConfB2" ); // NotRequired
const strReplaceWith2 = configs.get ( "StrConfC2" ); // NotRequired
const strFindWhat3 = configs.get ( "StrConfB3" ); // NotRequired
const strReplaceWith3 = configs.get ( "StrConfC3" ); // NotRequired
const strFindWhat4 = configs.get ( "StrConfB4" ); // NotRequired
const strReplaceWith4 = configs.get ( "StrConfC4" ); // NotRequired
const strFindWhat5 = configs.get ( "StrConfB5" ); // NotRequired
const strReplaceWith5 = configs.get ( "StrConfC5" ); // NotRequired
const strFindWhat6 = configs.get ( "StrConfB6" ); // NotRequired
const strReplaceWith6 = configs.get ( "StrConfC6" ); // NotRequired
const strFindWhat7 = configs.get ( "StrConfB7" ); // NotRequired
const strReplaceWith7 = configs.get ( "StrConfC7" ); // NotRequired
const strFindWhat8 = configs.get ( "StrConfB8" ); // NotRequired
const strReplaceWith8 = configs.get ( "StrConfC8" ); // NotRequired
const strFindWhat9 = configs.get ( "StrConfB9" ); // NotRequired
const strReplaceWith9 = configs.get ( "StrConfC9" ); // NotRequired
//// == Data Retrieving / ワークフローデータの参照 ==
// (Nothing. Retrieved via Expression Language in Config Retrieving)
//// == Calculating / 演算 ==
/// Replace All Text via Requests
/// Google Workspace for Developers > Google Docs for Developers > API v1
/// https://developers.google.com/docs/api/reference/rest/v1/documents/batchUpdate
/// https://developers.google.com/docs/api/reference/rest/v1/documents/request#ReplaceAllTextRequest
// request1, prepare
let i = 0;
let request1Obj = {};
request1Obj.requests = [];
if( strFindWhat1 !== "" ){
request1Obj.requests[i] = {};
request1Obj.requests[i].replaceAllText = {};
request1Obj.requests[i].replaceAllText.replaceText = strReplaceWith1;
request1Obj.requests[i].replaceAllText.containsText = {};
request1Obj.requests[i].replaceAllText.containsText.text = strFindWhat1;
request1Obj.requests[i].replaceAllText.containsText.matchCase = true;
// if change to "false", the find criteria become case insensitive.
i++;
}
if( strFindWhat2 !== "" ){
request1Obj.requests[i] = {};
request1Obj.requests[i].replaceAllText = {};
request1Obj.requests[i].replaceAllText.replaceText = strReplaceWith2;
request1Obj.requests[i].replaceAllText.containsText = {};
request1Obj.requests[i].replaceAllText.containsText.text = strFindWhat2;
request1Obj.requests[i].replaceAllText.containsText.matchCase = true;
// if change to "false", the find criteria become case insensitive.
i++;
}
if( strFindWhat3 !== "" ){
request1Obj.requests[i] = {};
request1Obj.requests[i].replaceAllText = {};
request1Obj.requests[i].replaceAllText.replaceText = strReplaceWith3;
request1Obj.requests[i].replaceAllText.containsText = {};
request1Obj.requests[i].replaceAllText.containsText.text = strFindWhat3;
request1Obj.requests[i].replaceAllText.containsText.matchCase = true;
// if change to "false", the find criteria become case insensitive.
i++;
}
if( strFindWhat4 !== "" ){
request1Obj.requests[i] = {};
request1Obj.requests[i].replaceAllText = {};
request1Obj.requests[i].replaceAllText.replaceText = strReplaceWith4;
request1Obj.requests[i].replaceAllText.containsText = {};
request1Obj.requests[i].replaceAllText.containsText.text = strFindWhat4;
request1Obj.requests[i].replaceAllText.containsText.matchCase = true;
// if change to "false", the find criteria become case insensitive.
i++;
}
if( strFindWhat5 !== "" ){
request1Obj.requests[i] = {};
request1Obj.requests[i].replaceAllText = {};
request1Obj.requests[i].replaceAllText.replaceText = strReplaceWith5;
request1Obj.requests[i].replaceAllText.containsText = {};
request1Obj.requests[i].replaceAllText.containsText.text = strFindWhat5;
request1Obj.requests[i].replaceAllText.containsText.matchCase = true;
// if change to "false", the find criteria become case insensitive.
i++;
}
if( strFindWhat6 !== "" ){
request1Obj.requests[i] = {};
request1Obj.requests[i].replaceAllText = {};
request1Obj.requests[i].replaceAllText.replaceText = strReplaceWith6;
request1Obj.requests[i].replaceAllText.containsText = {};
request1Obj.requests[i].replaceAllText.containsText.text = strFindWhat6;
request1Obj.requests[i].replaceAllText.containsText.matchCase = true;
// if change to "false", the find criteria become case insensitive.
i++;
}
if( strFindWhat7 !== "" ){
request1Obj.requests[i] = {};
request1Obj.requests[i].replaceAllText = {};
request1Obj.requests[i].replaceAllText.replaceText = strReplaceWith7;
request1Obj.requests[i].replaceAllText.containsText = {};
request1Obj.requests[i].replaceAllText.containsText.text = strFindWhat7;
request1Obj.requests[i].replaceAllText.containsText.matchCase = true;
// if change to "false", the find criteria become case insensitive.
i++;
}
if( strFindWhat8 !== "" ){
request1Obj.requests[i] = {};
request1Obj.requests[i].replaceAllText = {};
request1Obj.requests[i].replaceAllText.replaceText = strReplaceWith8;
request1Obj.requests[i].replaceAllText.containsText = {};
request1Obj.requests[i].replaceAllText.containsText.text = strFindWhat8;
request1Obj.requests[i].replaceAllText.containsText.matchCase = true;
// if change to "false", the find criteria become case insensitive.
i++;
}
if( strFindWhat9 !== "" ){
request1Obj.requests[i] = {};
request1Obj.requests[i].replaceAllText = {};
request1Obj.requests[i].replaceAllText.replaceText = strReplaceWith9;
request1Obj.requests[i].replaceAllText.containsText = {};
request1Obj.requests[i].replaceAllText.containsText.text = strFindWhat9;
request1Obj.requests[i].replaceAllText.containsText.matchCase = true;
// if change to "false", the find criteria become case insensitive.
i++;
}
if( i === 0 ){
throw new Error( "\n AutomatedTask UnexpectedError:" +
" FindWhat String {B} not found \n" );
}
let request1Uri = "https://docs.googleapis.com/v1/documents/" + strInputfileId + ":batchUpdate";
let request1 = httpClient.begin(); // HttpRequestWrapper
request1 = request1.authSetting( strAuthzSetting ); // with "Authorization: Bearer XX"
// https://questetra.zendesk.com/hc/en-us/articles/360024574471-R2300#HttpRequestWrapper
request1 = request1.body( JSON.stringify( request1Obj ), "application/json" );
// request1, try
const response1 = request1.post( request1Uri ); // HttpResponseWrapper
engine.log( " AutomatedTask ApiRequest1 Start: " + request1Uri );
const response1Code = response1.getStatusCode() + "";
const response1Body = response1.getResponseAsString() + "";
engine.log( " AutomatedTask ApiResponse Status: " + response1Code );
if( response1Code !== "200"){
throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
response1Code + "\n" + response1Body + "\n" );
}
// response1, parse
/* engine.log( response1Body ); // debug
{
"replies": [
{
"replaceAllText": {
"occurrencesChanged": 1
}
}
],
"writeControl": {
"requiredRevisionId": "ALm37BXbXm25CqcdhtXnbtCiMGCp9DM564YMvIznlpy75etxnogagBfmx-gwSaISrRhUHtynojSNHGsPzz9_eg"
},
"documentId": "18sAt2ROXl15fNtAS2A-mbt9jqZXjJim85eZdlYbw-0g"
}
*/
const response1Obj = JSON.parse( response1Body );
for( let j = 0; j < i; j++){
if( response1Obj.replies[j].replaceAllText.occurrencesChanged == undefined ){
engine.log( " AutomatedTask ApiResponse: Occurrences Changed 0 < " +
request1Obj.requests[j].replaceAllText.containsText.text );
}else{
engine.log( " AutomatedTask ApiResponse: Occurrences Changed " +
response1Obj.replies[j].replaceAllText.occurrencesChanged + " < " +
request1Obj.requests[j].replaceAllText.containsText.text );
}
}
//// == Data Updating / ワークフローデータへの代入 ==
// (nothing)
} //////// END "main()" /////////////////////////////////////////////////////////////////
/*
Notes:
- The task of rewriting the template file of the quotation and proposal is automated.
- If the FindWhat string is blank, the replacement process will not be performed.
- If the ReplaceWith string is blank, the found string will be erased.
- The File ID can be obtained from the URI or the sharing settings screen.
Notes-ja:
- 見積書や提案書の雛形ファイルを書き換える業務、等を自動化できます。
- 探索文字列が空白の場合、置換処理は行われません。
- 置換文字列が空白の場合、探索された文字列部は消去されます。
- File ID は URI や共有設定画面等から取得します。
APPENDIX-en
- Can refer to the "Process Log" for the number of replacements.
- The second replacement is searched for the document after the first replacement.
- When replacing with a multi-line string, the line feed code is converted to a space.
- How to set Config by EL expression (Workflow data reference method)
- https://questetra.zendesk.com/hc/en-us/articles/360024292872-R2272
- Google Drive: File, Copy
- https://support.questetra.com/addons/google-drive-file-copy-2021/
- Google Drive: GFile, Export as PDF
- https://support.questetra.com/addons/google-drive-gfile-export-as-pdf-2021/
- Setting example of "HTTP Authentication" (OAuth2)
- 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/drive
- https://www.googleapis.com/auth/documents
- Client ID, Consumer Secret:
- ( from https://console.developers.google.com/ )
- Redirect URLs: https://s.questetra.net/oauth2callback
APPENDIX-ja
- "プロセスログ" にて、探索文字列ごとの置換件数を参照可能です。
- 2番目の置換は、1番目置換の実行後ドキュメントに対して探索されます。
- 複数行文字列で置換することも可能ですが、改行コードがスペースに変換されたうえ挿入されます。
- EL式によるConfig設定方法(Workflowデータ参照方法)
- https://questetra.zendesk.com/hc/ja/articles/360024292872-R2272
- Google ドライブ: ファイル, コピー
- https://support.questetra.com/ja/addons/google-drive-file-copy-2021/
- Google ドライブ: Gファイル, PDFエクスポート
- https://support.questetra.com/ja/addons/google-drive-gfile-export-as-pdf-2021/
- "HTTP認証"(OAuth2)の設定例
- 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/drive
- https://www.googleapis.com/auth/documents
- Client ID, Consumer Secret:
- ( from https://console.developers.google.com/ )
- Redirect URLs: https://s.questetra.net/oauth2callback
*/
Download
2021-04-18 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/google-docs-document-replace-all-2021/
The Addon-import feature is available with Professional edition.
Notes
- The task of rewriting template files for quotations and proposals can be automated.
- If the FindWhat string is blank, the replacement process will not be performed.
- If the ReplaceWith string is blank, the found string will be erased.
- The File ID can be obtained from the URI or the sharing settings screen.
Capture


Appendix
- You can refer to the Process Log to see the number of replacements per search string.
- The second replacement is searched for in the document after the first replacement.
- When replacing with a multi-line string, the line feed code is converted to a space.
- How to set Config using EL expressions (Workflow data reference method)
- Google Drive: File, Copy
- Google Drive: GFile, Export as PDF
- Setting example of “HTTP Authentication” (OAuth2)
- Authorization Endpoint URL:
- Token Endpoint URL:
- Scope:
- Client ID, Consumer Secret:
- ( from https://console.developers.google.com/ )
- Redirect URLs: https://s.questetra.net/oauth2callback
Pingback: Google Docs: Document; Replace All Text – Questetra Support
Pingback: Google Drive: File, Copy – Questetra Support
Pingback: Understanding HTTP Authorization Settings – Questetra Support