Google Drive: File, Convert
Google Drive: File, Convert

Converts file in Google Drive and save as another MimeType file. Supports both Google Doc files and common files. File ID can be obtained from URI etc. (If Folder-ID not specified, the file will inherit any discoverable parents of the source file.)

2019-12-22 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/google-drive-file-convert/

Configs
  • A: Select OAuth2 Config Name (at [OAuth 2.0 Setting]) *
  • B: Set FILE-ID of Original * #{EL}
  • C-folder: Set FOLDER-ID of Drive which contains New File #{EL}
  • C-type: Set MimeType for New File * #{EL}
  • D: Set FILE NAME if you want to save as a different name #{EL}
  • E: Select STRING DATA for New File ID (update)
  • F: Select STRING DATA for New File Name (update)
  • H: Select STRING DATA for URL for Editing (update)
  • I: Select STRING DATA for URL for Viewing (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"
//  'drive.file' only gives you permission to files that your app has created
// Client ID:
//  ( from https://console.developers.google.com/ )
// Consumer Secret:
//  ( from https://console.developers.google.com/ )
//  *Redirect URLs: "https://s.questetra.net/oauth2callback"

// Refer to the file ID of the image from the URL in "Open in new window"
// If convert to "Google Docs", set MimeType to "application/vnd.google-apps.document"
// - https://developers.google.com/drive/api/v3/mime-types
// - https://developers.google.com/drive/api/v3/ref-export-formats

//////// START "main()" /////////////////////////////////////////////////////////////////

main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const oauth2   = configs.get( "conf_OAuth2"  ) + "";
const fileId   = configs.get( "conf_FileId")   + "";
const folderId = configs.get( "conf_FolderId") + "";
const mimeType = configs.get( "conf_MimeType") + "";
let   saveAs   = configs.get( "conf_SaveAs")   + "";
const dataIdE  = configs.get( "conf_DataIdE" ) + "";
const dataIdF  = configs.get( "conf_DataIdF" ) + "";
const dataIdH  = configs.get( "conf_DataIdH" ) + "";
const dataIdI  = configs.get( "conf_DataIdI" ) + "";
// 'java.lang.String' (String Obj) to javascript primitive 'string'
engine.log( " AutomatedTask Config: File ID: " + fileId );
if( fileId === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" + 
                   " String {B} (File ID) is required \n" );
}
if( folderId === "" ){
  engine.log( "  (File will be placed directly in My Drive folder.)" );
}
engine.log( " AutomatedTask Config: Folder ID: " + folderId );
if( mimeType === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" + 
                   " String {C-type} is required \n" );
}
engine.log( " AutomatedTask Config: New MimeType: " + mimeType );
engine.log( " AutomatedTask Config: Save As: " + saveAs );


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


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

/// post Copy Request
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/copy

let requestObj = {};
if( folderId !== "" ){
  requestObj.parents = [];
  requestObj.parents[0] = folderId;
}
if( saveAs !== "" ){
  requestObj.name = saveAs;
}
requestObj.mimeType = mimeType;
// https://developers.google.com/drive/api/v3/mime-types
// https://developers.google.com/drive/api/v3/ref-export-formats

apiRequest = apiRequest.body( JSON.stringify( requestObj ), "application/json" );
const apiUri = "https://www.googleapis.com/drive/v3/files/" + fileId + "/copy";
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 );
engine.log( " AutomatedTask ApiResponse: New File ID: " + responseObj.id );
engine.log( " AutomatedTask ApiResponse: New File MIME Type: " + responseObj.mimeType );


//// == Data Updating / ワークフローデータへの代入 ==
if( dataIdE !== "" ){
  engine.setDataByNumber( dataIdE, responseObj.id );
}
if( dataIdF !== "" ){
  engine.setDataByNumber( dataIdF, responseObj.name );
}
if( dataIdH !== "" ){
  engine.setDataByNumber( dataIdH, "https://docs.google.com/open?id=" + responseObj.id );
}
if( dataIdI !== "" ){
  engine.setDataByNumber( dataIdI, 
                          "https://drive.google.com/file/d/"+ responseObj.id + "/view" );
}

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

Download

Capture

Converts file in Google Drive and save as another MimeType file. Supports both Google Doc files and common files. File ID can be obtained from URI etc. (If Folder-ID not specified, the file will inherit any discoverable parents of the source file.)
OCR Workflow Automation Example
OCR Workflow Automation Example

Notes

  1. Refer to the file ID of the image etc. from the URL of the page displayed in “Open in new window”
  2. If not specified Folder ID, the file will inherit any discoverable parents of the source file.
  3. If you want to convert to “Google Docs”, set MimeType to “application/vnd.google-apps.document”
    1. https://developers.google.com/drive/api/v3/mime-types
    2. https://developers.google.com/drive/api/v3/ref-export-formats
  4. Convert from “MS-Office docx” to “Google Doc” to get the outline
  5. Convert images (JPEG, PNG, GIF, PDF) to “Google Docs” to process text extraction (OCR)
    1. Convert PDF and photo files to text
  6. OCR results can be saved as business data when used in combination with “Google Drive: GFile, Export as Text
  7. Conversion from “Google Docs” to “MS-Office docx” is not possible (as of December 2019)
  8. Conversion from “MS-Office docx” to “Google Spreadsheet” is not possible (as of December 2019)
  9. Image conversion such as “PNG” to “JPEG” is not possible (as of December 2019)
  10. There are cases where the conversion was successful, but failed (eg. PDF to IMG as of 2019)
  11. For some PDF, the recognition rate for text extraction (OCR) may be significantly reduced.

See also

Leave a Reply

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

%d bloggers like this: