我的功能适用于工作表的一行(第 4 行)。
但我想检查第 2 列和第 7 列中复选框所在的所有行。
我考虑过循环,但我想使用数组来获得更好、更高效的代码。
ss = SpreadsheetApp.getActiveSpreadsheet();
sheet = ss.getSheetByName("Feuille 1");
function alertbox(){
var box1= sheet.getRange(4, 2).getValue();
var box2 =sheet.getRange(4, 7).getValue();
if (box1 ==true && box2 == true) {
var activcell = sheet.getActiveCell();
showAlert(activcell);
}
};
function showAlert(activcell) {
var ui = SpreadsheetApp.getUi();
var result = ui.alert(
'Conflit entre un autre hôtel qui a réservé aux même dates que vous !',
'Vous êtes sûr de continuer ?',
ui.ButtonSet.YES_NO);
// utiliser les réponses des utilisateurs
if (result == ui.Button.YES) {
// clicked "Yes".
ui.alert('Voyez avec l autre hôtel !');
} else {
// User clicked "No" or "X".
ui.alert('Ok on enlève votre choix...');
activcell.setValue(false);
}
}
最佳答案
有几种构建数组的方法...
第一种方法是获取工作表的最后一行并在 getRange 方法中使用它
var lastRow = sheet.getLastRow();
var box1 = sheet.getRange(1, 2, lastRow).getValues();
第二个选项是在 getRange 中使用 a1Notation 并使用 array.filter 删除空白
var box1 = sheet.getRange("B:B").getValues().filter(String);
我建议使用第一个,这样您就知道您的数组长度相同。
在你得到你的 2 个数组后,我建议使用 for 循环来检查 box1 和 box2
for(var a = 0; a <= box1.length; a++){
if(box1[a] == true && box2[a] == true){
var activcell = sheet.getActiveCell(); //Switch from getActiveCell() to maybe a box3 to identify what you are trying to alert
showAlert(activcell);
}
}
这里有一些资源
关于javascript - 如何逐行在文档中的所有复选框中扩展我的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55824662/