Kasper Egelund
Kasper Egelund

Reputation: 151

How to move a shortcut in google drive

I need to move a shortcut file from one destination to another using google apps script. Usually I would move a file or folder like this:

function move(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sStamdata = ss.getSheetByName("New");
  var folderOld = DriveApp.getFolderById(folderOldId);
  var destination = DriveApp.getFolderById(folderId);
  var id = sStamdata.getRange('D77').getValue();
  var file = DriveApp.getFileById(id);

     folderOld.removeFile(file)
     destination.addFile(file)

But this doesn't work with shortcuts. Any ideas?

Upvotes: 1

Views: 439

Answers (1)

Tanaike
Tanaike

Reputation: 201388

How about this answer?

Modification points:

  • It seems that in the current stage, the shortcut cannot be moved using Drive service. I think that this might be resolve in the future update.
    • So as the current workaround, in this answer, I would like to propose to use the method of "Files: patch" Drive API v2, because Drive API of Advanced Google services is used.

When your script is modified, please modify as follows.

Modified script:

Before you run the script, please enable Drive API at Advanced Google services.

function move(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sStamdata = ss.getSheetByName("New");
//  var folderOld = DriveApp.getFolderById(folderOldId);
//  var destination = DriveApp.getFolderById(folderId);
  var id = sStamdata.getRange('D77').getValue();
//  var file = DriveApp.getFileById(id);

  Drive.Files.patch({parents: [{id: folderId}]}, id);  // Added
}
  • folderId is the destination folder ID.
  • Please confirm the file ID of the shortcut again.

Note:

  • For example, when the file ID of the shortcut is confirmed with the shared link using the browser, the file ID is ID of the original file. Please be careful this.

References:

Upvotes: 2

Related Questions