Google ドライブ: ファイル, ダウンロード (Google Drive: File, Download)
Googleドライブ内の任意ファイルをWorkflowデータとして取り込みます。ファイル名を変更して取り込むことも可能です。Googleファイル(Docs/Sheets/Slidesなど)が指定された場合はエラーとなります(Gファイル, エクスポート系のAddonをご利用ください)。
Configs
  • U: HTTP認証設定を選択してください *
  • A1: DownloadファイルのID(File-ID)をセットしてください *#{EL}
  • B1: Downloadファイルを別名で保存したい場合、ファイル名をセットしてください#{EL}
  • B2: Downloadファイルが格納されるファイル型データ項目を選択してください (追加) *
  • B3: ドライブ内でのファイル名を格納したい場合、文字列型データ項目を選択してください(更新)
  • B4: ファイル形式(MIME Type)を格納したい場合、文字列型データ項目を選択してください(更新)
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   strDownloadSaveas   = configs.get      ( "StrConfB1" );    // NotRequired
  if( strDownloadSaveas === "" ){
    engine.log( " AutomatedTask ConfigWarning:" +
                " Config {B1: DownloadSaveas} 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( strDownloadSaveas === "" ){
  strDownloadSaveas   = response1Obj.name;
}

/// Download a file stored on Google Drive.
/// Google Workspace for Developers > Google Drive for Developers > v3
/// https://developers.google.com/drive/api/v3/manage-downloads#download_a_file_stored_on_google_drive
// request2, prepare
let request2Uri = "https://www.googleapis.com/drive/v3/files/" + strInputfileId;
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( "supportsAllDrives", "true" );
    request2    = request2.queryParam( "alt", "media" ); // a download of content is being requested.
// 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(
                 strDownloadSaveas, 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:
- The File ID can be obtained from the sharing settings screen.
    - Refer to the file ID of the image from the URL in "Open in new window"

Notes-ja:
- File ID は共有設定画面等から取得できます。
    - 画像ファイルのIDは「新しいウィンドウで開く」のURLを参照します。

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/ja/addons/google-drive-file-download-2021/
Addonファイルのインポートは Professional でのみご利用いただけます

Notes

  • File ID は共有設定画面等から取得できます。
    • 画像ファイルのIDは「新しいウィンドウで開く」のURLを参照します。

Capture

Googleドライブ内の任意ファイルをWorkflowデータとして取り込みます。ファイル名を変更して取り込むことも可能です。Googleファイル(Docs/Sheets/Slidesなど)が指定された場合はエラーとなります(Gファイル, エクスポート系のAddonをご利用ください)。

Appendix

See also

コメントを残す

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

%d人のブロガーが「いいね」をつけました。