Google Drive File Copy

Copies a file on Google Drive and save it to the specified directory. Preparing a file to be a template and it will be a copy automatically at the Start of a new Issue.
Google Drive Files Copy
GoogleDrive-filesCopy-20171126-addonxml.txt
(md5:4a8b2fb389ac8c82ce55619cd5b4d2b7)

  • B’: File ID* of the copy source file stored in the Data Item selected at config B
  • C’: Folder ID** of save destination folder stored in Data Item selected at config C
  • D’: File name of generating file stored in the Data Item selected at config D
  • A new file will be generated as a copy of a file of File ID B’, and stored in a folder whose ID is C’
  • File ID of the generated file will be stored in the Data Item selected at config E
  • MimeType of the generated file will be stored in the Data Item selected at config F
  • The communication log will be stored in the Data Item selected at X
  • You need to register Project at Google API Manager beforehand
  • If there is no specified file ID or folder ID, an error will result
  • If the file name is not specified, it may be “Copy of original file name”
  • The file ID can be acquired at the Shareable link in the Sharing Setting of Google Drive files
  • * https://drive.google.com/file/d/023W9z6__EXAMPLE__D45EFmY1E/view
  • ** https://drive.google.com/drive/folders/0_example_EXAMPLE_example_dvamc

A. Set OAuth2 Config Name (at [OAuth 2.0 Setting])Single-line inputRequired
B. Select STRING/SELECT DATA for File IDData selectString(single line)orSelect(radio/select/search)Required
C. Select STRING/SELECT DATA for Folder ID to storeData selectString(single line)orSelect(radio/select/search)Required
D. Select STRING DATA for Name of the New FileData selectString(single line)
E. Select STRING DATA for New File ID (update)Data selectString(single line)
F. Select STRING DATA for New File MimeType (update)Data selectString(single line)
X. Select STRING DATA for Access Log (update)Data selectString(multiple line)
-variable namevariable label
Aconf_OAuth2Set OAuth2 Config Name (at [OAuth 2.0 Setting])
Bconf_DataIdBSelect STRING/SELECT DATA for File ID
Cconf_DataIdCSelect STRING/SELECT DATA for Folder ID to store
Dconf_DataIdDSelect STRING DATA for Name of the New File
Econf_DataIdESelect STRING DATA for New File ID (update)
Fconf_DataIdFSelect STRING DATA for New File MimeType (update)
Xconf_DataIdXSelect STRING DATA for Access Log (update)
// Google Drive Files Copy (ver. 20171126)
// (c) 2017, Questetra, Inc. (the MIT License)
// by Google Drive APIs Files get
// https://developers.google.com/drive/v3/reference/files/copy

// OAuth2 config sample at [OAuth 2.0 Setting]
// - 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" or "https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive.appdata"
// - Consumer Key: (Get by Google Developers Console)
// - Consumer Secret: (Get by Google Developers Console)


//// == Config Retrieving / 工程コンフィグの参照 ==
var oauth2 = configs.get( "conf_OAuth2" ) + "";
var dataIdB = configs.get( "conf_DataIdB" ) + "";
var dataIdC = configs.get( "conf_DataIdC" ) + "";
var dataIdD = configs.get( "conf_DataIdD" ) + "";
var dataIdE = configs.get( "conf_DataIdE" ) + "";
var dataIdF = configs.get( "conf_DataIdF" ) + "";
var dataIdX = configs.get( "conf_DataIdX" ) + "";
// convert 'java.lang.String' to 'javascript string'


//// == Data Retrieving / ワークフローデータの参照 ==
var fileId = "";
if( engine.findDataDefinitionByNumber( dataIdB ).matchDataType( "SELECT_SINGLE" ) ){
    fileId = engine.findDataByNumber( dataIdB ).get(0).getValue() + "";
}else{
    fileId = engine.findDataByNumber( dataIdB ) + "";
}
var folderId = "";
if( engine.findDataDefinitionByNumber( dataIdC ).matchDataType( "SELECT_SINGLE" ) ){
    folderId = engine.findDataByNumber( dataIdC ).get(0).getValue() + "";
}else{
    folderId = engine.findDataByNumber( dataIdC ) + "";
}
var newFileName = "";
if ( dataIdD !== "" ){ 
  if ( engine.findDataByNumber( dataIdD ) !== null ){ 
    newFileName = engine.findDataByNumber( dataIdD ) + "";
  }
}


//// == Calculating / 演算 ==
// preparing for API Request
var apiRequest = httpClient.begin(); // HttpRequestWrapper
// com.questetra.bpms.core.event.scripttask.HttpClientWrapper

// Request HEADER (OAuth2 Token, HTTP Basic Auth, etc)
var token  = httpClient.getOAuth2Token( oauth2 );
apiRequest = apiRequest.bearer( token );

// Request PATH (https://example.com/abc/def/)
var apiUri = "https://www.googleapis.com/drive/v3/files/";
    apiUri += fileId;
    apiUri += "/copy";

// Request QUERY (?a=b)
apiRequest = apiRequest.queryParam( "supportsTeamDrives", true );

// Request BODY (JSON, Form Parameters, etc)
var requestObj = {};
  requestObj.parents = [];
  requestObj.parents[0] = folderId;
  if ( newFileName !== "" ){ 
    requestObj.name = newFileName;
  }
apiRequest = apiRequest.body( JSON.stringify( requestObj ), "application/json" );

// Access to the API (POST, GET, PUT, etc)
var response = apiRequest.post( apiUri ); // HttpResponseWrapper
var httpStatus = response.getStatusCode() + "";
var accessLog = "---POST request--- " + httpStatus + "\n";
accessLog += response.getResponseAsString() + "\n";
var responseObj = JSON.parse( response.getResponseAsString() );

// Retrieve Properties from Response-JSON
var newFileId = responseObj.id;
var newFileMimetype = responseObj.mimeType;

// Error Handling
// (no set)

//// == Data Updating / ワークフローデータへの代入 ==
if ( dataIdE !== "" ){ engine.setDataByNumber( dataIdE, newFileId ); }
if ( dataIdF !== "" ){ engine.setDataByNumber( dataIdF, newFileMimetype ); }
if ( dataIdX !== "" ){ engine.setDataByNumber( dataIdX, accessLog ); }