Google Drive #GFile: Export as Text

Google Drive: GFile, Export as Text
Google Drive: GFile, Export as Text
Stores a Google file as Workflow data after converting it to a Text file. It is also possible to change the file name. If a file other than a Google file (Docs/Slides) is specified an error will occur.
Configs
  • U: Select HTTP_Authz Setting *
  • A1: Set FILE-ID in Drive *#{EL}
  • B1: Set File Name if you want to save as a different name#{EL}
  • B2: Select FILE DATA that stores Exported File (append) *
  • B3: Select STRING DATA that stores File Name in Drive (update)
  • B4: Select STRING DATA that stores Mime-Type in Drive (update)
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" );
  }
let   strSaveas           = configs.get      ( "StrConfB1" );    // NotRequired
  if( strSaveas         === "" ){
    engine.log( " AutomatedTask ConfigWarning:" +
                " Config {B1: ExportedSaveas} is empty" );
  }
const filesPocketDownload  = configs.getObject( "SelectConfB2" ); /// REQUIRED
const strPocketNameOnDrive = configs.getObject( "SelectConfB3" ); // NotRequired
const strPocketMimetype    = configs.getObject( "SelectConfB4" ); // NotRequired


//// == Data Retrieving / ワークフローデータの参照 ==
let filesAttached = engine.findData( filesPocketDownload ); // java.util.ArrayList
if( filesAttached === null ) {
  engine.log( " AutomatedTask FilesArray {B2} (empty)" );
  filesAttached = new java.util.ArrayList();
}else{
  engine.log( " AutomatedTask FilesArray {B2}: " +
              filesAttached.size() + " files" );
}


//// == Calculating / 演算 ==
/// Gets a file's metadata by ID.
/// Google Workspace for Developers > Google Drive for Developers > v3
/// https://developers.google.com/drive/api/v3/reference/files/get
// request1, prepare
let request1Uri = "https://www.googleapis.com/drive/v3/files/" + strInputfileId;
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.queryParam( "supportsAllDrives", "true" );
// request1, try
const response1     = request1.get( 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
{
  "kind": "drive#file",
  "id": "0BwSW_lixFpUuQ2JyV1pqU3VzTGs",
  "name": "Questetra-Intro-en.pdf",
  "mimeType": "application/pdf"
}
*/
const response1Obj = JSON.parse( response1Body );
engine.log( " AutomatedTask ApiResponse: File Name OnDrive: " + response1Obj.name );
engine.log( " AutomatedTask ApiResponse: File MIME-Type: " + response1Obj.mimeType );
if( strSaveas === "" ){
  strSaveas   = response1Obj.name + ".txt";
}

/// Export as PDF from Google Drive to BPMS.
/// Google Workspace for Developers > Google Drive for Developers > v3
/// https://developers.google.com/drive/api/v3/reference/files/export
let request2Uri = "https://www.googleapis.com/drive/v3/files/" + strInputfileId + "/export";
let request2    = httpClient.begin(); // HttpRequestWrapper
    request2    = request2.authSetting( strAuthzSetting ); // with "Authorization: Bearer XX"
    // https://questetra.zendesk.com/hc/en-us/articles/360024574471-R2300#HttpRequestWrapper
    request2    = request2.queryParam( "mimeType", "text/plain" );
// request2, try
const response2     = request2.get( request2Uri ); // HttpResponseWrapper
engine.log( " AutomatedTask ApiRequest2 Start: " + request2Uri );
const response2Code = response2.getStatusCode() + "";
engine.log( " AutomatedTask ApiResponse Status: " + response2Code );
if( response2Code !== "200"){
  const response2Body = response2.getResponseAsString() + "";
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
                    response2Code + "\n" + response2Body + "\n" );
}
// response2, parse
const fileTmp = new com.questetra.bpms.core.event.scripttask.NewQfile(
                 strSaveas, response2.getContentType(), response2.getResponse()
                );
filesAttached.add( fileTmp );


//// == Data Updating / ワークフローデータへの代入 ==
engine.setData( filesPocketDownload,    filesAttached );

if( strPocketNameOnDrive !== null ){
  engine.setData( strPocketNameOnDrive, response1Obj.name );
}
if( strPocketMimetype    !== null ){
  engine.setData( strPocketMimetype,    response1Obj.mimeType );
}

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


/*
Notes:
- Text extraction of contracts and manuals created with Google Docs is automated.
- The File ID can be obtained from the URI or the sharing settings screen.
- The exported content is limited to 10MB.
- To extract string data (string type) from text file (file type), 
    - Converter (Text File to String type data)
    - https://support.questetra.com/addons/converter-textfile-to-string/
Notes-ja:
- Google ドキュメントで作成された契約書やマニュアルのテキスト情報抽出を自動化できます。
- File ID は URI や共有設定画面等から取得します。
- Exportされるファイルのサイズ上限は 10MB です。
- テキスト(ファイル型)から文字列データ(文字列型)を抽出するには別途自動工程を配置します。
    - コンバータ (テキストファイル to 文字型データ)
    - https://support.questetra.com/ja/addons/converter-textfile-to-string/

APPENDIX-en
- 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.readonly
    - Client ID, Consumer Secret:
        - ( from https://console.developers.google.com/ )
        - Redirect URLs: https://s.questetra.net/oauth2callback
APPENDIX-ja
- "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.readonly
    - Client ID, Consumer Secret:
        - ( from https://console.developers.google.com/ )
        - Redirect URLs: https://s.questetra.net/oauth2callback
*/

Download

2021-04-08 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/google-drive-gfile-export-as-text-2021/
The Addon-import feature is available with Professional edition.

Notes

  • Text extraction of contracts and manuals created with Google Docs is automated.
  • The File ID can be obtained from the URI or the sharing settings screen.
  • The exported content is limited to 10MB.
  • To extract string data (string type) from text file (file type):

Capture

Stores Google file as Workflow data with converting to a Text file. It is also possible to change the file name. If a file other than a Google file (Docs/Slides) is specified, an error will occur.

Appendix

See also

Google Drive: File, Download

4 thoughts on “Google Drive #GFile: Export as Text”

  1. Pingback: Google Drive: GFile, Export as Text – Questetra Support

  2. Pingback: Google Drive: GFile, Export as MS-Office – Questetra Support

  3. Pingback: Google Drive: GFile, Export as PDF – Questetra Support

  4. Pingback: Google Drive: File, Convert – Questetra Support

Leave a Reply

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

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top