从自定义函数设置单元格值引发您没有调用setValue的权限 [英] Setting cell value from custom function throws You do not have permission to call setValue
本文介绍了从自定义函数设置单元格值引发您没有调用setValue的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Google Sheets脚本中,我正在尝试将值从工作表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's guidelines有关表内公式的使用,具体说明:
只读(可以使用大多数get*()方法,但不能使用set*())。 无法打开其他电子表格(SpreadsheetApp.openById()或SpreadsheetApp.openByUrl())。
和:
如果您的自定义函数引发错误消息,您无权调用X服务。该服务需要用户授权,因此无法在自定义函数中使用。
要使用上面列出的服务以外的服务,请创建运行Apps脚本函数的自定义菜单,而不是编写自定义函数。从菜单触发的函数将在必要时要求用户授权,因此可以使用所有应用程序脚本服务。
因此,按照David的建议,您应该使用自定义菜单,而不是公式。
这篇关于从自定义函数设置单元格值引发您没有调用setValue的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文