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

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

Google App Script - UI alert message to validate cell color

问题

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

//验证单元格颜色 #00ff40(绿色)
var cell = "B6"; // 单元格。
var colors = ["#00ff40", "#ffffff"]; // 要验证的两种颜色。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet(); // 或者使用 ss.getSheets()[0];
var range = sheet.getRange(cell);
var currentColor = range.getBackground();
var color = colors.find(e => e != currentColor);

if (currentColor == "#ffffff") {

  ui.alert("颜色不是绿色。");
  shUserForm.getRange("B6").activate();

  return false;

}

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.

//Validating Cell Color #00ff40 (green)
   var cell = "B6"; // The cell.
   var colors = ["#00ff40", "#ffffff"]; // The two colors to validate.

   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var sheet = ss.getActiveSheet(); // or ss.getSheets()[0];
   var range = sheet.getRange(cell);
   var currentColor = range.getBackground();
   var color = colors.find(e => e != currentColor);
   
  if (currentColor=="#ffffff"){
      
      ui.alert("Color is not green.");
      shUserForm.getRange("B6").activate();
      
      return false;

  }

  return true;

答案1

得分: 0

这对我有效:

function testie() {
  var cell = "B6";
  var colors = ["#00ff40", "#ffffff"];
  const ui = SpreadsheetApp.getUi();
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getActiveSheet();
  var range = sheet.getRange(cell);
  var currentColor = range.getBackground();
  if (currentColor == "#ffffff") {
    ui.alert("颜色不是绿色。");
    shUserForm.getRange("B6").activate();
    return false;
  }
  return true;
}
英文:

This works for me:

function testie() {
  var cell = "B6";
  var colors = ["#00ff40", "#ffffff"];
  const ui = SpreadsheetApp.getUi();
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getActiveSheet();
  var range = sheet.getRange(cell);
  var currentColor = range.getBackground();
  if (currentColor == "#ffffff") {
    ui.alert("Color is not green.");
    shUserForm.getRange("B6").activate();
    return false;
  }
  return true;
}

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:

确定