【问题标题】:Setting cell value from custom function throws You do not have permission to call setValue从自定义函数设置单元格值抛出您无权调用 setValue
【发布时间】:2017-10-08 17:38:23
【问题描述】:

在 Google 表格脚本中,我试图将值从表格 A 中的一个单元格复制到表格 B 中的另一个单元格。

问题是,当我在 oogle Sheets 脚本编辑器中启动 avgUtilisationPerWeek() 函数时,代码正在完成这项工作,它按照定义设置单元格的值。

但是当我使用公式=avgUtilisationPerWeek() 从目标表调用函数时,它会报告#ERROR

您无权调用 setValue(第 108 行)。

在自定义函数中,我试图设置工作表中其他单元格的值,而不仅仅是放置公式的单元格。

这是源代码:

function avgUtilisationPerWeek(){
    for(var i = 1; i < 14; i++) {
        Utilities.sleep(3000);
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Dynamic Weekly Utilisation Report').getRange('D1').setValue(i);
        t1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Dynamic Weekly Utilisation Report').getRange('L4').getValue();
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Graph Q1 2017').getRange((String.fromCharCode('A'.charCodeAt(0) + i)).concat('24')).setValue(t1);

        v = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Dynamic Weekly Utilisation Report').getRange('L5').getValue();
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Graph Q1 2017').getRange((String.fromCharCode('A'.charCodeAt(0) + i)).concat('29')).setValue(v);

        g = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Dynamic Weekly Utilisation Report').getRange('L6').getValue();
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Graph Q1 2017').getRange((String.fromCharCode('A'.charCodeAt(0) + i)).concat('34')).setValue(g);

        t2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Dynamic Weekly Utilisation Report').getRange('L7').getValue();
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Graph Q1 2017').getRange((String.fromCharCode('A'.charCodeAt(0) + i)).concat('39')).setValue(t2);
  }
}

我看了这篇文章,但没有找到解决办法。

Google Script setValue permission

Transferring cell data in Google Script?

【问题讨论】:

    标签: google-apps-script google-sheets google-sheets-formula custom-function setvalue


    【解决方案1】:

    Google's guidelines 关于在工作表中使用公式的具体说明:

    只读(可以使用大多数 get*() 方法,但不能使用 set*())。 无法打开其他电子表格(SpreadsheetApp.openById() 或 SpreadsheetApp.openByUrl())。

    和:

    如果您的自定义函数抛出错误消息您没有权限调用 X 服务。该服务需要用户授权,因此不能在自定义函数中使用。

    要使用上述服务以外的服务,请创建一个运行 Apps 脚本函数的自定义菜单,而不是编写自定义函数。从菜单触发的功能会在必要时请求用户授权,从而可以使用所有 Apps 脚本服务。

    因此,正如 David 所建议的,您应该使用自定义菜单,而不是公式。

    【讨论】:

      【解决方案2】:

      我认为您无法从工作表中启动它 - 除非我错了。对您有用的是创建一个单独的函数 onOpen() 来获得一个自定义菜单,该菜单允许您直接从工作表中运行您的函数。以下内容对您有用吗?

      详细如下:

      function onOpen() {
      var ui = SpreadsheetApp.getUi();
      
        ui.createMenu('Custom Functions')
            .addItem('Copy Function', 'avgUtilisationPerWeek')
            .addToUi();
      
        SpreadsheetApp.flush();    
      }
      

      您的代码与下面相同,但我只是出于自己的可视化目的对其进行了一些清理,这样做是可选的。

      function avgUtilisationPerWeek(){
      
          var ss = SpreadsheetApp.getActiveSpreadsheet();
          var sheet1 = ss.getSheetByName('Dynamic Weekly Utilisation Report');
          var sheet2 = ss.getSheetByName('Graph Q1 2017');
      
        for(var i = 1; i < 14; i++) {
          Utilities.sleep(3000); 
      
          sheet1.getRange('D1').setValue(i);
          t1 = sheet1.getRange('L4').getValue();
          sheet2.getRange((String.fromCharCode('A'.charCodeAt(0) + i)).concat('24')).setValue(t1);
      
          v = sheet1.getRange('L5').getValue();
          sheet2.getRange((String.fromCharCode('A'.charCodeAt(0) + i)).concat('29')).setValue(v);
      
          g = sheet1.getRange('L6').getValue();
          sheet2.getRange((String.fromCharCode('A'.charCodeAt(0) + i)).concat('34')).setValue(g);
      
          t2 = sheet1.getRange('L7').getValue();
          sheet2.getRange((String.fromCharCode('A'.charCodeAt(0) + i)).concat('39')).setValue(t2);
        }
      }
      

      【讨论】:

      • 如果我将 onOpen 函数的代码放在脚本编辑器中,并使用 =onOpen() 在工作表中运行该函数,我会收到消息“无法从此上下文中调用 SpreadsheetApp.getUi()。 (第 16 行)。”
      • 您应该能够从菜单运行它 - onOpen() 在 UI 的其余部分旁边创建一个菜单。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 2013-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多