Google ドキュメント: 文書; テキストの全置換 (Google Docs: Document; Replace All Text)

検索テキストを置換テキストに全置換します。文字列の検索では大文字と小文字が区別されます。置き換え件数はログ出力されます。多くの場合、上流に自動工程『Google ドライブ: ファイル; コピー』が配置されます。

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

Configs
  • A: 通信許可の設定名([OAuth2.0設定]メニュー)を選択してください *
  • B: Docsファイルの Document-ID をセットしてください (ファイルURI内の44文字) * #{EL}
  • C: 検索する文字列をセットしてください (例 “XXXX-XX-XX”) * #{EL}
  • D: 置換後の文字列をセットしてください #{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. たとえば「契約書テンプレート」に存在する全ての “XXXX-XX-XX” を “2019-09-25” に全置換します。
  2. 多くの場合、上流に自動工程『Google ドライブ: ファイル; コピー』が配置されます。
  3. 複数の置換パターンを一括して設定したい場合は『複数のテキスト全置換』をご利用ください。
  4. [Google Blog] 新しい Google Docs API を使用したワークフローの管理とプロセスの自動化 (2019-02-22)
  5. [Google Blog] Google Docs API で文書処理が高速に

See also

コメントを残す

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

%d