我在谷歌文档电子表格中有三个脚本。在此电子表格中,在column H
(或column 8
)中,如果我键入x
,脚本会将其更改为当天日期。
几天后,每个日期column H
都从一个日期变成了一个数字。数字看起来像这样:40492, 40494, 40511
。
我不确定是什么原因造成的。也许这是我的脚本有问题。我已经把它们贴在下面了。有任何想法吗?
这是第一个:
function onEdit(e) {
var colorA = "yellow";
var colorB = "#dddddd";
var colorC = "#dddddd";
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Purchase Orders");
var range = e.source.getActiveRange();
var sheetName = SpreadsheetApp.getActiveSheet().getName();
if (sheetName == "Purchase Orders") {
// 3 is column C
if (range.getColumn() == 7 && range.getValue() != "") {
var r = range.getRow() + 1;
sheet.getRange("A" + r + ":G" + r).setBackgroundColor(colorC);
}
}
var col = e.source.getActiveRange().getColumn();
if(col == 8 || col == 7) {
var rows = sheet.getMaxRows();
//column C
var rangeC = sheet.getRange("H1:H"+rows);
var valuesC = rangeC.getValues();
//column H range
var rangeH = sheet.getRange("G1:G"+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
var hRow = colorH[row];
if (valuesC[row][0] != "" && valuesH[row][0] == "") {
hRow[0] = colorA;
} else if (valuesH[row][0] != "") {
hRow[0] = colorB;
}
}
sheet.getRange("G1:G" + rows).setBackgroundColors(colorH);
}
}
这是第二个:
function onEdit(e) {
var colorA = "yellow";
var colorB = "#dddddd";
var colorC = "#dddddd";
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Purchase Orders");
var range = e.source.getActiveRange();
var sheetName = SpreadsheetApp.getActiveSheet().getName();
if (sheetName == "Purchase Orders") {
// 3 is column C
if (range.getColumn() == 3 && range.getValue() != "") {
sheet.insertRowAfter(range.getRow());
var r = range.getRow() + 1;
sheet.getRange("A" + r + ":H" + r).setBackgroundColor(colorC);
}
}
var col = e.source.getActiveRange().getColumn();
if(col == 3 || col == 8) {
var rows = sheet.getMaxRows();
//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
var hRow = colorH[row];
if (valuesC[row][0] != "" && valuesH[row][0] == "") {
hRow[0] = colorA;
} else if (valuesH[row][0] != "") {
hRow[0] = colorB;
}
}
sheet.getRange("H1:H" + rows).setBackgroundColors(colorH);
}
}
这是第三个:
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.setNumberFormat("MM/dd/yyyy")
r.setValue(Utilities.formatDate(new Date(), "MST", "yyyy-MM-dd"));
}
}