javascript - 您无权使用 copyTo

标签 javascript google-apps-script google-sheets google-sheets-formula google-sheets-custom-function

我正在尝试将一个范围从一张纸复制到另一张纸(同时保留公式)。我使用 copyTo 编写了一个简单的脚本:

function copyRangeAcrossSheets(source_sheet,source_range,target_sheet,target_range) {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var source_sheet = spreadsheet.getSheetByName(source_sheet);
  var target_sheet = spreadsheet.getSheetByName(target_sheet);
  var source_range = source_sheet.getRange(source_range);
  var target_range = target_sheet.getRange(target_range);
  source_range.copyTo(target_range);
}

我称之为:

=copyRangeAcrossSheets("TEST_source","A1:A3","TEST_target","A1:A3")

我收到以下错误:

You do not have the permission to call copyTo

我做了一些挖掘,发现 functions have to use special triggers (installable) in order to modify another file 。然而,我在这里修改同一个文件。

问题1:为什么copyTo在这里失败?

问题2:如何在无需定义可安装触发器的情况下解决该问题?(我只想在保留公式的同时复制范围)

最佳答案

为什么会失败?

您不能修改其他任何需要通过自定义功能授权的文档。原因是您的函数是以匿名用户身份执行的,无法获得编辑您的其他表格或文档所需的授权。

引用:https://developers.google.com/apps-script/guides/sheets/functions#using_apps_script_services

这段代码专门针对您:

Spreadsheet: Read only (can use most get*() methods, but not set*()). Cannot open other spreadsheets (SpreadsheetApp.openById() or SpreadsheetApp.openByUrl()).

另外:

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.

如何解决这个问题?

编写一个通过触发器或手动执行的 Apps 脚本函数,您可以使用 onEditonChange 触发器,或基于时间的触发器。您甚至可以在需要时在 Apps Script IDE 中手动运行该函数。这是 Apps 脚本的预期行为。

关于javascript - 您无权使用 copyTo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36837005/

相关文章:

javascript - 字体大小计算错误

javascript - D3 中的过滤选项

javascript - Facebook 点赞/分享当前 URL

javascript - 纠结于如何检测 JSON 中的用户机器人并返回具有给定条件的用户

arrays - 使用转置公式谷歌表格对多行求和

python - Google API(表格)API 错误代码 403。权限不足 : Request had insufficient authentication scopes

javascript - Google Apps 脚本 (V8);为什么我不能在 onOpen 内部使用对象实例?

javascript - 显示时日期格式错误

javascript - Google 脚本共享权

javascript - googleapis 失败(在 JWT Auth 之后),出现 TypeError : authClient. request is not a function