0

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.

AlexB
  • 2,164
  • 6
  • 27
  • 61

1 Answers1

1

You should return a JSON object from the Google Script function.

var newFile = DriveApp.getFileById(fileId).makeCopy('Copy of '+title);
return {
  title: title, 
  url: newFile.getUrl()
}
Amit Agarwal
  • 10,910
  • 1
  • 32
  • 43
  • perfect, this has worked like a charm, would this work on the user's side, the add-on is installable from the Chrome Store and G Suit? – AlexB Mar 06 '19 at 14:16
  • This solution works in my local Google Drive, but I have just published a new version of my add-on and when I try it from the different account I constantly get an error that `You do not have permission to access the requested document.` – AlexB Mar 06 '19 at 16:45