在 Google Sheet 的 Google Apps 脚本中,我有这段带有 OnEdit 触发器的代码。它根据我从下拉列表中选择的值更新单元格的注释,该下拉列表是使用“数据验证”(范围下拉列表)为单元格创建的。
下拉列表中包含的单独值,有些值前面有一个 + 例如。 +1 修改器
我尝试格式化下拉范围的值 例如。 ="+1 修改器
此后,按上述方式修改时,下拉选择的值本身不会导致错误。
但是以下 Apps 脚本(用于将所选下拉值设置为注释)中断并且未添加注释,显示错误:
function addNotescToRange() {
// Get the "GraveLayout" sheet
const sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("GraveLayout");
// Define the range (A1:B2) (to set Notes for each cell based off its value)
const range1 = sheet1.getRange("A1:B2");
// Get the values in the range as a two-dimensional array
const values = range1.getValues();
// Loop through each cell in the range
for (let i = 0; i < values.length; i++) {
for (let j = 0; j < values[i].length; j++) {
const cell = range1.getCell(i + 1, j + 1); // +1 for zero-based indexing
const value = values[i][j];
// Set the note for the cell using its value
cell.setNote(value);
}
}
}
我手动添加了撇号,将其附加到代码最后部分的值中,但注释仍然错误:
// Set the note for the cell using its value
cell.setNote('"' & value);
在 GAS 中,串联使用的不是
&
,而是 +
。也就是说,如果要在值前添加撇号,那么代码不应该是cell.setNote(' " ' & value)
,而是cell.setNote(" ' " + value)
。
但这对您没有帮助,因为问题的出现是因为您在下拉菜单的值范围中使用 ="+1 to modifier" 。
如果这个特定的拼写(作为公式)对您来说并不重要,则使用拼写 '+1 修饰符(作为文本)。
在这种情况下,该值被正确理解,不会发生错误,并且注释设置正确。
我更正了电子表格中 E4:E5 范围内的值,现在一切正常。