谷歌表单,根据单元格值TRUE/FALSE弹出消息



每当单元格从FALSE变为TRUE时,我试图让Google Sheets显示一条弹出消息。我已经找到了一些使用onEdit函数的方法。这里的问题是,有问题的单元格并没有真正被编辑,而是由于另一组单元格满足某些条件而更改了值。这意味着onEdit函数在值更改时不会运行。

我试过这个脚本,但它什么都没用:

//popup message
function alertMessageYesNoButton() {
var result = SpreadsheetApp.getUi().alert("Are you sure you want to send an alert about?", SpreadsheetApp.getUi().ButtonSet.YES_NO);
SpreadsheetApp.getActive().toast(result);
}
function sendMessageYesNo(e){
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var cellValue = sheet.getRange(4, 17).getValue();
if (cellValue == 'TRUE'){
alertMessageYesNoButton();
}else{}
}

如果有人能想出一些有效的办法,我会非常感激

根据您的以下回复,

当使用以下公式检查其他三个复选框时,

单元格D17从FALSE变为TRUE:{"";SUMPRODUCT(B16:B18)=COUNTA(B16:B18)}

在这种情况下,检查";B16"B17"B18";?当这反映在你的展示脚本中时,下面的修改如何?我认为当手动选中这些复选框时,可以使用onEdit触发器。

在该修改中;B16:B18";的";Sheet1";则运行alertMessageYesNoButton()的脚本。因此,当您测试此项时,请选中"的所有复选框;B16:B18";的";Sheet1";。这样,脚本就运行了。

模式1:

function onEdit(e) {
const sheetName = "Sheet1"; // This is from your script.
const checkBoxRange = "B16:B18"; // This is from your reply.
const { range } = e;
const sheet = range.getSheet();
const r = sheet.getRange(checkBoxRange);
const rowStart = r.getRow();
const rowEnd = rowStart + r.getNumRows() - 1;
const colStart = r.getColumn();
const condition1 = sheet.getSheetName() == sheetName;
const condition2 = range.rowStart >= rowStart && range.rowEnd <= rowEnd && range.columnStart == colStart;
const condition3 = r.getValues().every(([b]) => b === true);
if (!condition1 || !condition2 || !condition3) return;
alertMessageYesNoButton(); // This is your script.
}

模式2:

function onEdit(e) {
const sheetName = "Sheet1"; // This is from your script.
const checkBoxRange = "B16:B18"; // This is from your reply.
const { range } = e;
const sheet = range.getSheet();
const r = sheet.getRange(checkBoxRange);
const rowStart = r.getRow();
const rowEnd = rowStart + r.getNumRows() - 1;
const colStart = r.getColumn();
const condition1 = sheet.getSheetName() == sheetName;
const condition2 = range.rowStart >= rowStart && range.rowEnd <= rowEnd && range.columnStart == colStart;
const condition3 = r.getValues().every(([b]) => b === true);
if (!condition1 || !condition2 || !condition3) return;
// This is your script.
var cellValue = sheet.getRange(4, 17).getValue();
if (cellValue == 'TRUE') {
alertMessageYesNoButton();
}
}

参考:

  • 简单触发器

添加:

根据您的以下回复,

这对我列出的示例非常有效,但我尝试在同一张工作表中使用同一脚本的多个实例,但它总是只在其中一个中有效。因此,假设我有相同情况的例子(B16:B18(、(B13:B15(、(D13:D15(和(D16:D18(。只有当我选中与最后一个功能相对应的框时(从上到下(,才会弹出消息。

从你的问题和回复中,我提出了一个答案,作为";B16:B18";。但从你的回复来看,当你的复选框是";B16:B18"B13:B15"D13:D15"D16:D18";,下面的示例脚本怎么样?

示例脚本:

function onEdit(e) {
const sheetName = "Sheet1"; // This is from your script.
const checkBoxRanges = ["B16:B18", "B13:B15", "D13:D15", "D16:D18"]; // This is from your reply.
const { range } = e;
const sheet = range.getSheet();
const actSheetName = sheet.getSheetName();
const res = checkBoxRanges.find(checkBoxRange => {
const r = sheet.getRange(checkBoxRange);
const rowStart = r.getRow();
const rowEnd = rowStart + r.getNumRows() - 1;
const colStart = r.getColumn();
const condition1 = actSheetName == sheetName;
const condition2 = range.rowStart >= rowStart && range.rowEnd <= rowEnd && range.columnStart == colStart;
const condition3 = r.getValues().every(([b]) => b === true);
return condition1 && condition2 && condition3;
});
if (!res) return;
Browser.msgBox(`Checked checkboxes of ${res}`); // Here, you can see the checkbox group that was checked.
alertMessageYesNoButton(); // This is your script.
}
  • 在这种情况下,通过给定const checkBoxRanges = ["B16:B18", "B13:B15", "D13:D15", "D16:D18"],脚本检测选中的复选框组,并将范围显示为示例

相关内容

最新更新