Google Drive: GFile, Export as Text

Google Drive: GFile, Export as Text
Google Drive: GFile, Export as Text

Exports text information from a GFile in Google Drive. If a file other than a Google file (Docs / Slides) is specified, an error will occur (Please use File Download). File ID can be obtained from URI.

2019-12-20 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/google-drive-gfile-export-as-text/

Configs
  • A: Select OAuth2 Config Name (at [OAuth 2.0 Setting]) *
  • B: Set ID of FILE to be Exported * #{EL}
  • C: Select STRING DATA for Text (update) *
  • D: Select STRING DATA for File Name on Google Drive (update)
Script (click to open)

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

// Google Sheets, not supported (As of 2019)
// Google スプレッドシート はサポートされていません(2019年現在)


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

//// == Config Retrieving / 工程コンフィグの参照 ==
const oauth2  = configs.get( "conf_OAuth2"  ) + "";
const fileId  = configs.get( "conf_FileId") + "";
const dataIdC = configs.get( "conf_DataIdC" ) + "";
const dataIdD = configs.get( "conf_DataIdD" ) + "";
engine.log( " AutomatedTask Config: File ID: " + fileId );
if( fileId === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" + 
                   " String {B} (File ID) is required \n" );
}


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


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

/// get File Name (Metadata)
let apiRequest = httpClient.begin(); // HttpRequestWrapper
apiRequest = apiRequest.bearer( token );
apiRequest = apiRequest.queryParam( "supportsAllDrives", "true" );
// This parameter will only be effective until June 1, 2020.
// see https://developers.google.com/drive/api/v3/reference/files/get
const apiUri = "https://www.googleapis.com/drive/v3/files/" + fileId;
engine.log( " AutomatedTask Trying: GET " + apiUri );
const response = apiRequest.get( 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 );
engine.log( " AutomatedTask ApiResponse: File Name: " + responseObj.name );
engine.log( " AutomatedTask ApiResponse: File MIME Type: " + responseObj.mimeType );

/// get Text
let apiRequest2 = httpClient.begin(); // HttpRequestWrapper
apiRequest2 = apiRequest2.bearer( token );
apiRequest2 = apiRequest2.queryParam( "mimeType", "text/plain" );
// https://developers.google.com/drive/api/v3/ref-export-formats
const apiUri2 = "https://www.googleapis.com/drive/v3/files/" + 
                 fileId + "/export";
engine.log( " AutomatedTask Trying: GET " + apiUri2 );
const response2 = apiRequest2.get( apiUri2 );
const responseCode2 = response2.getStatusCode() + "";
engine.log( " AutomatedTask ApiResponse: Status " + responseCode2 );
if( responseCode2 !== "200"){
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
         responseCode2 + "\n" + response2.getResponseAsString() + "\n" );
}
let strText = response2.getResponseAsString() + "";
engine.log( " AutomatedTask ApiResponse: Text Info exported" );


//// == Data Updating / ワークフローデータへの代入 ==
engine.setDataByNumber( dataIdC, strText );
if( dataIdD !== "" ){
  engine.setDataByNumber( dataIdD, responseObj.name );
}


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

Download

Capture

Exports text information from a GFile in Google Drive. If a file other than a Google file (Docs / Slides) is specified, an error will occur (Please use File Download). File ID can be obtained from URI.

Notes

  1. Google Sheets, not supported (As of 2019)

See also

1 thought on “Google Drive: GFile, Export as Text”

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

Leave a Reply

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

Scroll to Top

Discover more from Questetra Support

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

Continue reading