如何缩短 OR || 列表Google Apps 脚本中的运算符
How To Shorten a List of OR || Operators in an Google Apps Script
我正在尝试自定义 中的以下 GAS 脚本,以便在触发事件(在单元格中手动输入值)时将背景颜色添加到 sheet 的所有列。
我想到了使用数组而不是多个 || OR 语句,但我不确定该怎么做。
ziganotschka 脚本(使用我自定义的多个 || OR 语句):
function onEdit(e) {
var c = e.range.getColumn();
if(c == 1 || c == 2){
var text = e.value;
var sheet = SpreadsheetApp.getActive().getActiveSheet();
var range = sheet.getRange(1,1,sheet.getLastRow(),2);
var values = range.getValues();
var array = [];
var row = e.range.getRow();
for (var i =0; i <values.length; i++){
if(row!=(i+1))
{
array.push(values[i][0]);
}
}
if(array.indexOf(text)==-1){
var backgrounds = range.getBackgrounds();
var color = getRandomColor();
while(backgrounds.indexOf(color)>-1){
color = getRandomColor();
}
buildConditionalFormatting(text, color)
}
}
}
function getRandomColor() {
var letters = '0123456789abcdef';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function buildConditionalFormatting(text, color){
var sheet = SpreadsheetApp.getActiveSheet();
var formattingRange = sheet.getRange("A:B");
var rule = SpreadsheetApp.newConditionalFormatRule()
.whenTextEqualTo(text)
.setBackground(color)
.setRanges([formattingRange])
.build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);
}
简而言之,我的要求是如何从下面的 allColumns
数组中获取任何索引(???)以用作下面的 if 语句中的列号?
function onEdit(e) {
const allColumns = [1, 2];
var c = e.range.getColumn();
if(c == ??? ){
我的目标是找到一种方法来避免必须使用多个 ||每列的 OR 语句(可能是无限数量的列,取决于触发器甚至涉及在任何单元格中输入文本)。
我一直在寻找一种使用数组代替 || 的方法OR 语句,到目前为止,我已经从这篇文章 How to Easily Shorten Long Lists of OR(||) Operators in Your Code.
中找到了 array.includes() 方法
但是我的代码不起作用,这是我试过的方法:
function onEdit(e) {
const allColumns = [1, 2];
var c = e.range.getColumn();
if(c == allColumns.includes(e)){
我也考虑过 Getting a random value from a JavaScript array, and How to create an array containing 1...N。但也无济于事。
我的代码:
function onEdit(e) {
const allColumns = [1, 2];
const random = Math.floor(Math.random() * allColumns.length);
var c = e.range.getColumn();
if(c == random ){
我的代码:
function onEdit(e) {
const allColumns = _.range(1, 2);
const random = Math.floor(Math.random() * allColumns.length);
var c = e.range.getColumn();
if(c == random ){
e
是事件对象 {}
和 various properties
c
是列号。
if(c == allColumns.includes(e)){
不正确。该脚本要求检查 allColumns
是否包含对象 e
。那总是 return false
。正确的语法是:
if(allColumns.includes(c)){
练习 Array.includes.
我正在尝试自定义
我想到了使用数组而不是多个 || OR 语句,但我不确定该怎么做。
ziganotschka 脚本(使用我自定义的多个 || OR 语句):
function onEdit(e) {
var c = e.range.getColumn();
if(c == 1 || c == 2){
var text = e.value;
var sheet = SpreadsheetApp.getActive().getActiveSheet();
var range = sheet.getRange(1,1,sheet.getLastRow(),2);
var values = range.getValues();
var array = [];
var row = e.range.getRow();
for (var i =0; i <values.length; i++){
if(row!=(i+1))
{
array.push(values[i][0]);
}
}
if(array.indexOf(text)==-1){
var backgrounds = range.getBackgrounds();
var color = getRandomColor();
while(backgrounds.indexOf(color)>-1){
color = getRandomColor();
}
buildConditionalFormatting(text, color)
}
}
}
function getRandomColor() {
var letters = '0123456789abcdef';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function buildConditionalFormatting(text, color){
var sheet = SpreadsheetApp.getActiveSheet();
var formattingRange = sheet.getRange("A:B");
var rule = SpreadsheetApp.newConditionalFormatRule()
.whenTextEqualTo(text)
.setBackground(color)
.setRanges([formattingRange])
.build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);
}
简而言之,我的要求是如何从下面的 allColumns
数组中获取任何索引(???)以用作下面的 if 语句中的列号?
function onEdit(e) {
const allColumns = [1, 2];
var c = e.range.getColumn();
if(c == ??? ){
我的目标是找到一种方法来避免必须使用多个 ||每列的 OR 语句(可能是无限数量的列,取决于触发器甚至涉及在任何单元格中输入文本)。
我一直在寻找一种使用数组代替 || 的方法OR 语句,到目前为止,我已经从这篇文章 How to Easily Shorten Long Lists of OR(||) Operators in Your Code.
中找到了 array.includes() 方法但是我的代码不起作用,这是我试过的方法:
function onEdit(e) {
const allColumns = [1, 2];
var c = e.range.getColumn();
if(c == allColumns.includes(e)){
我也考虑过 Getting a random value from a JavaScript array, and How to create an array containing 1...N。但也无济于事。
我的代码:
function onEdit(e) {
const allColumns = [1, 2];
const random = Math.floor(Math.random() * allColumns.length);
var c = e.range.getColumn();
if(c == random ){
我的代码:
function onEdit(e) {
const allColumns = _.range(1, 2);
const random = Math.floor(Math.random() * allColumns.length);
var c = e.range.getColumn();
if(c == random ){
e
是事件对象 {}
和 various properties
c
是列号。
if(c == allColumns.includes(e)){
不正确。该脚本要求检查 allColumns
是否包含对象 e
。那总是 return false
。正确的语法是:
if(allColumns.includes(c)){
练习 Array.includes.