Google Drive: File, Copy
Google Drive: File, Copy
Creates a copy of a file and saves it in Google Drive. You can save it with any name you like. Note that the name of the file does not necessarily have to be unique within the folder.
Configs
  • U: Select HTTP_Authz Setting *
  • A1: Set FILE-ID of Original *#{EL}
  • B1: Set FOLDER-ID of Drive which contains New File#{EL}
  • B2: Set FILE NAME if you want to save as a different name#{EL}
  • C1: Select STRING that stores New File Name (update)
  • C2: Select STRING that stores New File ID (update)
  • C3: Select STRING that stores URL for Editing (update)
  • C4: Select STRING that stores URL for Viewing (update)
  • C5: Select STRING that stores MimeType of New File (update)
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" );
  }
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
*/

Download

2021-04-19 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/google-drive-file-copy-2021/
The Addon-import feature is available with Professional edition.

Notes

  • It is used when creating a document by duplicating the template file. (Invoices, Proposals, etc.)
  • The file ID of the image refers to the URL in “Open in new window”
  • If the folder ID is not specified the folder information of the original file is inherited
  • This automated task (Addon) does not support duplication and saving on My Drive.

Capture

Creates a copy of a file in Google Drive. It supports saving as an arbitrary name. Note that the name of the file is not necessarily unique within a folder.
Creates a copy of a file in Google Drive. It supports saving as an arbitrary name. Note that the name of the file is not necessarily unique within a folder.

Appendix

See also

2 thoughts on “Google Drive #File: Copy”

  1. Pingback: Google Drive Files Copy – Questetra Support

  2. Pingback: Google Docs: Document, Replace All – Questetra Support

Leave a Reply

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

%d bloggers like this: