I am trying to return the URL and the Title of the copied file for the user of the add-on to be able to have some information as well as an ability to open the file directly from the add-on after making a copy.
my Code.gs file function:
function createFileCopy(id){
var file = id.split('.');
var docName = DriveApp.getFilesByName(file[0]);
while (docName.hasNext()) {
var file = docName.next();
var fileId = file.getId();
var fileName = file.getName();
}
var sheet = TEMPLATES_DATA;
var data = sheet.getRange(1, 9, sheet.getLastRow()-1, 1).getValues();
var pos = data.map(function (obj) { return obj[0]; }).indexOf(id);
if(pos > -1){
var val = sheet.getRange("J" + (pos + 1)).getValue() + 1;
var title = sheet.getRange("A" + (pos + 1)).getValue();
sheet.getRange("J" + (pos + 1)).setValue(val);
}
return DriveApp.getFileById(fileId).makeCopy('Copy of '+title).getUrl();
}
the function in my javascript.html file should trigger on success is as follows
function fileCopySuccess(fileInfo){
var content = '';
if( fileInfo != null){
content += '<h2>' + fileInfo['title'] + '</h2>';
content += '<a class="button action" href="' + fileInfo['url'] + '" target="_blank">Open File</a>';
$('#downloadBtn').html(content).removeClass('error');
} else {
$('#downloadBtn').html('');
}
}
Currently, I only get undefined
on both, title
and URL
I am sure that I have to add some method to the end after the makeCopy()
to pass the array to my fileCopySuccess
, but do not know what and how.
Please help.