Google App Script – UI弹出消息以验证单元格颜色

huangapple go评论91阅读模式
英文:

Google App Script - UI alert message to validate cell color

问题

我正在尝试在单元格颜色为白色时返回一个ui.alert消息,但在绿色时返回true。我能够使其在单元格颜色为白色时返回消息,但当我将单元格颜色更改为绿色时,它仍然返回消息。我有一种感觉这是一个简单的问题,但我已经尝试了几个小时来找出问题,但没有成功。

  1. //验证单元格颜色 #00ff40(绿色)
  2. var cell = "B6"; // 单元格。
  3. var colors = ["#00ff40", "#ffffff"]; // 要验证的两种颜色。
  4. var ss = SpreadsheetApp.getActiveSpreadsheet();
  5. var sheet = ss.getActiveSheet(); // 或者使用 ss.getSheets()[0];
  6. var range = sheet.getRange(cell);
  7. var currentColor = range.getBackground();
  8. var color = colors.find(e => e != currentColor);
  9. if (currentColor == "#ffffff") {
  10. ui.alert("颜色不是绿色。");
  11. shUserForm.getRange("B6").activate();
  12. return false;
  13. }
  14. return true;

注意:请确保你的代码中有 shUserForm 变量的定义,否则可能会导致错误。

英文:

I am trying to return a ui.alert message when a cell color is white but return true if green. I was able to get it to return the message when the cell color is white but when I change the cell color to green, it still returns the message. I have a feeling it is something simple but I have tried for hours trying to figure out the issue but no luck.

  1. //Validating Cell Color #00ff40 (green)
  2. var cell = "B6"; // The cell.
  3. var colors = ["#00ff40", "#ffffff"]; // The two colors to validate.
  4. var ss = SpreadsheetApp.getActiveSpreadsheet();
  5. var sheet = ss.getActiveSheet(); // or ss.getSheets()[0];
  6. var range = sheet.getRange(cell);
  7. var currentColor = range.getBackground();
  8. var color = colors.find(e => e != currentColor);
  9. if (currentColor=="#ffffff"){
  10. ui.alert("Color is not green.");
  11. shUserForm.getRange("B6").activate();
  12. return false;
  13. }
  14. return true;

答案1

得分: 0

这对我有效:

  1. function testie() {
  2. var cell = "B6";
  3. var colors = ["#00ff40", "#ffffff"];
  4. const ui = SpreadsheetApp.getUi();
  5. var ss = SpreadsheetApp.getActive();
  6. var sheet = ss.getActiveSheet();
  7. var range = sheet.getRange(cell);
  8. var currentColor = range.getBackground();
  9. if (currentColor == "#ffffff") {
  10. ui.alert("颜色不是绿色。");
  11. shUserForm.getRange("B6").activate();
  12. return false;
  13. }
  14. return true;
  15. }
英文:

This works for me:

  1. function testie() {
  2. var cell = "B6";
  3. var colors = ["#00ff40", "#ffffff"];
  4. const ui = SpreadsheetApp.getUi();
  5. var ss = SpreadsheetApp.getActive();
  6. var sheet = ss.getActiveSheet();
  7. var range = sheet.getRange(cell);
  8. var currentColor = range.getBackground();
  9. if (currentColor == "#ffffff") {
  10. ui.alert("Color is not green.");
  11. shUserForm.getRange("B6").activate();
  12. return false;
  13. }
  14. return true;
  15. }

huangapple
  • 本文由 发表于 2023年5月29日 02:50:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定