Home > OS >  How to use shortcutDetails (to get the id of the file the shortcut point at) in Google Apps Script
How to use shortcutDetails (to get the id of the file the shortcut point at) in Google Apps Script

Time:09-29

I'm trying to copy documents from a folder, and inside my folder I have some shortcuts. I want to copy the original file in my new folder so I'm trying to get the id of the real file using the id of the shortcut.

I tried to use shortcutDetails but I'm not sure how it works.

Here is my code:

function TestFunction() {
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName("test");
  var id = "My shortcut Id";
  var file = DriveApp.getFileById(id);
  Logger.log(file.getMimeType());
  var a = file.shortcutDetails.targetId();
  Logger.log(a);
}

This is the result when I put my shortcut id:

enter image description here

CodePudding user response:

In your script, how about the following modification?

From:

var a = file.shortcutDetails.targetId();

To:

var a = file.getMimeType() == MimeType.SHORTCUT ? file.getTargetId() : id;

By this modification, when file is the shortcut file, the target file ID is returned. When file is not the shortcut file, id is returned.

Reference:

  • Related