由于权限问题,我创建的自定义公式无法正常工作,但在遵循指南后,我仍然无法理解问题所在。
我编写了一个自定义公式“SUMIFALL”,并将其发布给与addon相同域的其他组成员。出于某种原因,当我们尝试在不包含实际脚本(但通过插件包含)的电子表格中使用它(包括我在内)时,它会生成以下错误:
错误 您无权调用 getActiveUser(第 37 行)。
遵循这些关于谷歌表格中自定义公式访问权限的指南:https ://developers.google.com/apps-script/guides/sheets/functions#advanced
特别是关于床单:
只读(可以使用大多数 get*() 方法,但不能使用 set*())。无法打开其他电子表格(SpreadsheetApp.openById() 或 SpreadsheetApp.openByUrl())。
我试图确保只使用 Get 函数,但它仍然会产生错误。编码:
/**
* For each cell in {searchCol} that contains the cell content, it adds
* the correspond value from the same row in {sumCol}
* {criterion} is optional if want to add a condition the the {sumCol} value also need to fullfill.
* @param {searchCol} input The column to check and iterate over.
* @param {sumCol} input The correspond column from which we add to sum.
* @param {criterion} input An optional field - adding a constraint on {sumCol} values.
* @return The sum of all cells that met the requirements.
* @customfunction
*/
function SUMIFALL(searchCol, sumCol, criterion){
var checkCriteria = criterion != null ? encapsulateCriterion(criterion) : function(val) { return true; };
var arrayOfSum = searchCol.map(function(cell){
var sum = 0;
for(var i=0; i<searchCol.length; i++){
var traffic = Number(sumCol[i]);
var phrase = searchCol[i].toString();
var found = phrase.search(new RegExp("\\b("+cell+")\\b","gi")) != -1;
sum += checkCriteria(traffic) && found ? traffic : 0;
}
return sum;
});
return arrayOfSum;
}
/**
* For each cell in {searchCol} that contains the word {cell} content
* adds the correspond value from the same row in {sumCol}
* {criterion} is optional if want to add a condition the the {sumCol} value also need to fullfill.
* @param {cell} A cell that contains the word to be searched.
* @param {searchCol} input The column to check and iterate over.
* @param {sumCol} input The correspond column from which we add to sum.
* @param {criterion} input An optional field - adding a constraint on {sumCol} values.
* @return The sum of all cells that met the requirements.
* @customfunction
*/
function SUMIFREGEX(cell, searchCol, sumCol, criterion){
var checkCriteria = criterion != null ? encapsulateCriterion(criterion) : function(val) { return true; };
var sum = 0;
for(var i=0; i<searchCol.length; i++){
var traffic = Number(sumCol[i]);
var phrase = searchCol[i].toString();
var found = phrase.search(new RegExp("\\b("+cell+")\\b","gi")) != -1;
sum += checkCriteria(traffic) && found ? traffic : 0;
}
return sum;
}
function encapsulateCriterion(criterion){
var criteriaSplit = criterion.search(/[0-9]/gi);
var criteria = criterion.substring(0,criteriaSplit);
var number = Number(criterion.substring(criteriaSplit));
return function(val){
return criteria == '=' ? val == number :
criteria == '<' ? val < number :
criteria == '>' ? val > number :
criteria == '<=' ? val <= number :
criteria == '>=' ? val >= number :
criteria == '<>' ? val != number : true;
}
}
他们还在他们的页面上提到:
如果您的自定义函数抛出错误消息您没有权限调用 X 服务,则该服务需要用户授权,因此无法在自定义函数中使用。
有谁知道可能是什么问题?对工作表内容的唯一操作是在提供的范围内,这与他们在页面上提供的示例非常相似。
提前致谢