// 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" );
}
const strOutputfolderId = configs.get ( "StrConfB1" ); // NotRequired
if( strOutputfolderId === "" ){
engine.log( " AutomatedTask ConfigWarning:" +
" Config {B1: FolderId} is empty" );
}
let strOutputfileSaveas = configs.get ( "StrConfB2" ); // NotRequired
const strPocketOutputfileName = configs.getObject( "SelectConfC1" ); // NotRequired
const strPocketOutputfileId = configs.getObject( "SelectConfC2" ); // NotRequired
const strPocketOutputfileEdit = configs.getObject( "SelectConfC3" ); // NotRequired
const strPocketOutputfileView = configs.getObject( "SelectConfC4" ); // NotRequired
const strPocketOutputfileMime = configs.getObject( "SelectConfC5" ); // NotRequired
//// == Data Retrieving / ワークフローデータの参照 ==
// (Nothing. Retrieved via Expression Language in Config Retrieving)
//// == Calculating / 演算 ==
/// Google Workspace for Developers > Google Drive for Developers > v3
/// https://developers.google.com/drive/api/v3/reference/files/copy
// request1, prepare
let request1Obj = {};
if( strOutputfolderId !== "" ){
request1Obj.parents = [];
request1Obj.parents[0] = strOutputfolderId;
}
if( strOutputfileSaveas !== "" ){
request1Obj.name = strOutputfileSaveas;
}
let request1Uri = "https://www.googleapis.com/drive/v3/files/" +
strInputfileId + "/copy";
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 = request1.body( JSON.stringify( request1Obj ), "application/json" );
// request1, try
const response1 = request1.post( 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": "1x9rZrt-Iyj2QBewzeLF1P7fG-plXaKMyCnJSFAT-d1g",
"name": "Questetra-Intro-en",
"mimeType": "application/vnd.google-apps.document"
}
*/
const response1Obj = JSON.parse( response1Body );
engine.log( " AutomatedTask ApiResponse: NewFile ID: " + response1Obj.id );
engine.log( " AutomatedTask ApiResponse: MIME Type: " + response1Obj.mimeType );
//// == Data Updating / ワークフローデータへの代入 ==
if( strPocketOutputfileName !== null ){
engine.setData( strPocketOutputfileName, response1Obj.name );
}
if( strPocketOutputfileId !== null ){
engine.setData( strPocketOutputfileId, response1Obj.id );
}
if( strPocketOutputfileEdit !== null ){
engine.setData( strPocketOutputfileEdit, "https://docs.google.com/open?id=" +
response1Obj.id );
}
if( strPocketOutputfileView !== null ){
engine.setData( strPocketOutputfileView, "https://drive.google.com/file/d/" +
response1Obj.id + "/view" );
}
if( strPocketOutputfileMime !== null ){
engine.setData( strPocketOutputfileMime, response1Obj.mimeType );
}
} //////// END "main()" /////////////////////////////////////////////////////////////////
/*
Notes:
- It is used when creating a document by duplicating the template. (Invoices, Proposals, etc.)
- Refer to the file ID of the image from the URL in "Open in new window"
- If Folder-ID not specified, the file will inherit any discoverable parents of the source file.
- This automated task (Addon) does not support to duplicate to "My Drive".
Notes-ja:
- ひな型ファイルを複製して個別文書を作成する際等に利用します。(請求書、見積書、提案書など)
- 画像ファイルのIDは「新しいウィンドウで開く」のURLを参照します。
- 保存フォルダが無指定の場合、元ファイルの所属フォルダ情報が継承されます。
- この自動工程(アドオン)では、"マイドライブ" への複製保存はできません。
APPENDIX-en
- Folders cannot be copied or converted.
- 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
- 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
- Client ID, Consumer Secret:
- ( from https://console.developers.google.com/ )
- Redirect URLs: https://s.questetra.net/oauth2callback
*/
Pingback: Google Drive Files Copy – Questetra Support
Pingback: Google Docs: Document, Replace All – Questetra Support