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();
}
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.
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.
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.
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.
Data retrieved using DriveApp.getFilesByName()
is FileIterator. In this case, file is retrieved by using next()
.
There are 2 patterns for the sample script.
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();
}
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();
}
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.
dialog()
.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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With