Google 张工作表从 url 获取文件名

Google sheets get file name from url

我正在尝试在 google 工作表中创建一个函数以从 url link 获取工作簿名称。

function getFileName(id) {
var file = SpreadsheetApp.openByUrl(id)
var fileName = file.getName();    
return fileName;  
}

我收到消息

“错误 您没有调用 openByUrl 的权限

我试过了

  var file = DriveApp.getFileById(id);

我收到了同样的消息

"No item with the given ID could be found, or you do not have permission to access it. "

您不能将其用作自定义函数,因为它需要授权。

Google Documentation

If your custom function throws the error message You do not have permission to call X service., the service requires user authorization and thus cannot be used in a custom function.

To use a service other than those listed above, create a custom menu that runs an Apps Script function instead of writing a custom function. A function that is triggered from a menu will ask the user for authorization if necessary and can consequently use all Apps Script services.