Google 脚本设置值
Google script setValue
我正在尝试学习如何使用 Google 脚本在 Google 电子表格中操作单元格。我了解到,
Custom functions return values, but they cannot set values outside the cells they are in. In most circumstances, a custom function in cell A1 cannot modify cell A5. However, if a custom function returns a double array, the results overflow the cell containing the function and fill the cells below and to the right of the cell containing the custom function. You can test this with a custom function containing return [[1,2],[3,4]];
所以我在单元格 C14 中调用函数
function test(input){
var secondCell = SpreadsheetApp.getActiveSheet().getRange("C14").setValue("Ahoj");
return secondCell.getValue();
}
仍然出现错误
You do not have permission to call setValue
我什至无法在调用函数的单元格中设置数据。
有人知道为什么这不起作用吗?
编辑:
我读过可能重复的内容Why can't you use setValue in a custom function?
但这并不能解决我的问题。
我不想编辑其他单元格。我只想编辑包含公式 的原始单元格。根据引用的文本,如果它是包含公式的原始单元格,我应该可以编辑单元格。但是我的例子返回错误,即使它只访问它自己。
您的代码没有意义,您在 C14 中以编程方式将值设置为 "Ahoj",然后获取此已知变量,并尝试 return 将其设置在您刚刚尝试的单元格中将值设置为 "Ahoj",这将替换您刚刚用于 运行 这个函数的函数。这几乎是一个悖论。
你弄糊涂了,你可以得到C14的值,但是那个函数在C14中调用不了,或者你想把C14的值设置为"Ahoy",在那个函数中all你需要的是:
function test(input){
return 'Ahoj';
}
如果您希望另一个单元格具有 C14 的值,则:
function test(input){
return SpreadsheetApp.getActiveSheet().getRange('C14').getValue();
}
returned 值将是新的单元格值,它不是通过 setValue
完成的。
我正在尝试学习如何使用 Google 脚本在 Google 电子表格中操作单元格。我了解到,
Custom functions return values, but they cannot set values outside the cells they are in. In most circumstances, a custom function in cell A1 cannot modify cell A5. However, if a custom function returns a double array, the results overflow the cell containing the function and fill the cells below and to the right of the cell containing the custom function. You can test this with a custom function containing return [[1,2],[3,4]];
所以我在单元格 C14 中调用函数
function test(input){
var secondCell = SpreadsheetApp.getActiveSheet().getRange("C14").setValue("Ahoj");
return secondCell.getValue();
}
仍然出现错误
You do not have permission to call setValue
我什至无法在调用函数的单元格中设置数据。
有人知道为什么这不起作用吗?
编辑:
我读过可能重复的内容Why can't you use setValue in a custom function?
但这并不能解决我的问题。
我不想编辑其他单元格。我只想编辑包含公式 的原始单元格。根据引用的文本,如果它是包含公式的原始单元格,我应该可以编辑单元格。但是我的例子返回错误,即使它只访问它自己。
您的代码没有意义,您在 C14 中以编程方式将值设置为 "Ahoj",然后获取此已知变量,并尝试 return 将其设置在您刚刚尝试的单元格中将值设置为 "Ahoj",这将替换您刚刚用于 运行 这个函数的函数。这几乎是一个悖论。
你弄糊涂了,你可以得到C14的值,但是那个函数在C14中调用不了,或者你想把C14的值设置为"Ahoy",在那个函数中all你需要的是:
function test(input){
return 'Ahoj';
}
如果您希望另一个单元格具有 C14 的值,则:
function test(input){
return SpreadsheetApp.getActiveSheet().getRange('C14').getValue();
}
returned 值将是新的单元格值,它不是通过 setValue
完成的。