gpt4 book ai didi

Javascript 合并 if 语句

转载 作者:行者123 更新时间:2023-11-30 13:37:05 27 4
gpt4 key购买 nike

有人告诉我应该合并我的 if 语句。我不确定该怎么做?另外,这个脚本还有什么问题吗?它用于谷歌文档脚本。

function onEdit(e) {
var colorA = "yellow";
var colorB = "#dddddd";
var colorC = "#dddddd";

var sheet = e.source.getActiveSheet();
var range = e.source.getActiveRange();

// 3 is column C
if (range.getColumn() == 3) {
if (range.getValue() != "") {
sheet.insertRowAfter(range.getRow());
var r = range.getRow() + 1;
sheet.getRange("A" + r + ":H" + r).setBackgroundColor(colorC);
}
}

if (e.source.getActiveRange().getColumn() == 3 ||
e.source.getActiveRange().getColumn() == 8) {
var rows = sheet.getMaxRows();
//two ranges
//column C
var rangeC = sheet.getRange("C1:C"+rows);
var valuesC = rangeC.getValues();
//column H range
var rangeH = sheet.getRange("H1:H"+rows);
var colorH = rangeH.getBackgroundColors();
var valuesH = rangeH.getValues();

//iterate over each row in column C and H
//then change color
for (var row = 0; row < valuesC.length; row++) {
//check for columnC and column H
if (valuesC[row][0] != "" && valuesH[row][0] == "") {
colorH[row][0] = colorA;
} else if (valuesH[row][0] != "") {
colorH[row][0] = colorB;
}
}
sheet.getRange("H1:H" + rows).setBackgroundColors(colorH);
}
}





Here is the other one




ss = SpreadsheetApp.getActiveSpreadsheet();


function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [ {name: "New PO", functionName: "NewPO"}];
ss.addMenu("New PO", menuEntries);
}


function NewPO() {
SpreadsheetApp.getActiveSheet().insertRowsBefore(1,6);


// Adjust this range accordingly, these are the cells that will be
// copied. Format is getRange(startRow, startCol, numRows, numCols)
ss.getSheetByName("PO Form").getRange(1, 1, 6, 8)
.copyTo(SpreadsheetApp.getActiveSheet().getRange(1, 1, 6, 8));
}


function onEdit(e) {
var ss = e.source.getActiveSheet();
var r = e.source.getActiveRange();
// 1 is A, 2 is B, ... 8 is H
if (r.getColumn() == 8 && r.getValue() == "x") {
r.setValue(Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"));
}
}

最佳答案

除了 murray 注意到的,还有几个重复相同表达的例子:

if (e.source.getActiveRange().getColumn() == 3 ||
e.source.getActiveRange().getColumn() == 8) {

可能是:

var col = e.source.getActiveRange().getColumn();
if(col == 3 || col == 8) {

这在较小程度上适用于:

if (valuesC[row][0] != "" && valuesH[row][0] == "") {
colorH[row][0] = colorA;
} else if (valuesH[row][0] != "") {
colorH[row][0] = colorB;
}

这可能是(例如):

var hRow = colorH[row];
if (valuesC[row][0] != "" && valuesH[row][0] == "") {
hRow[0] = colorA;
} else if (valuesH[row][0] != "") {
hRow[0] = colorB;
}

关于Javascript 合并 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4395764/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com