Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script Duplicate A File

I am simply trying to duplicate an open sheets file. I tried so many variation without success. The below is an example. Would value any assistance. Thank You.

function copyDocs() {
    var file = DriveApp.getFilesByName('My Income Statement');
    file.makeCopy();
}
like image 688
Tacitus Avatar asked Jul 14 '17 05:07

Tacitus


People also ask

Can I use urlfetchapp with Google Drive files?

Only users with permission to open the file in Google Drive can access the URL. You can use this URL in a browser to download the file, but you can't use to fetch the file with UrlFetchApp. If you want the contents of the file in the script, use getBlob (). String — The URL that can be used to download the file.

How to open the copied file using dialog on spreadsheet?

This sample is for opening the copied file using a dialog on spreadsheet. Since the copied file is opened as new window, please permit to open popup window. Copy and paste following script to container-bound script of spreadsheet. Run dialog (). Push a copy button on the dialog box on spreadsheet.

How to copy a file with the same filename?

If there are files with the same filename and you want to copy one of them, you can use following sample script. This sample copies the file using the fileID. The fileID can be retrieved as follows. This sample is for opening the copied file using a dialog on spreadsheet.

How to retrieve the fileID of a copied file?

The fileID can be retrieved as follows. This sample is for opening the copied file using a dialog on spreadsheet. Since the copied file is opened as new window, please permit to open popup window. Copy and paste following script to container-bound script of spreadsheet. Run dialog (). Push a copy button on the dialog box on spreadsheet.


1 Answers

Data retrieved using DriveApp.getFilesByName() is FileIterator. In this case, file is retrieved by using next().

There are 2 patterns for the sample script.

Sample script 1

Pattern 1 :

If the filename is only one in your Drive, you can use following sample script. This sample copies the file using the filename.

function copyDocs() {
  var file = DriveApp.getFilesByName('My Income Statement').next();
  file.makeCopy();
}

Pattern 2 :

If there are files with the same filename and you want to copy one of them, you can use following sample script. This sample copies the file using the fileID. The fileID can be retrieved as follows.

For document,

https://docs.google.com/document/d/### File ID ###/edit

For spreadsheet,

https://docs.google.com/spreadsheets/d/### File ID ###/edit

Sample script :

function copyDocs() {
  var file = DriveApp.getFileById("### File ID ###");
  file.makeCopy();
}

Sample script 2

This sample is for opening the copied file using a dialog on spreadsheet. Since the copied file is opened as new window, please permit to open popup window.

  1. Copy and paste following script to container-bound script of spreadsheet.
  2. Run dialog().
  3. Push a copy button on the dialog box on spreadsheet.

By above flow, the file with fileId is copied and opened as new window.

function dialog() {
  var data = '<input type="button" value="copy" onclick="google.script.run.withSuccessHandler(openfile).filecopy();"><script>function openfile(url) {window.open(url);}</script>';
  var html = HtmlService.createHtmlOutput(data);
  SpreadsheetApp.getUi().showModalDialog(html, 'Sample dialog');
}

function filecopy(){
  var fileId = "### File ID ###";
  return DriveApp.getFileById(fileId).makeCopy().getUrl();
}
like image 86
Tanaike Avatar answered Oct 06 '22 01:10

Tanaike