[英]Color range of cells based on values in different sheet
I have 13 sheets in Google Sheets, 12 sheets having names in row and dates in column header. 我在Google表格中有13个工作表,其中12个工作表中的行名和列标题中的日期。
I want the whole column to be grey-formatted based on holidays listed in a "HolidayList"
sheet. 我希望根据
"HolidayList"
表中列出的假期将整个列设置为灰色格式。 I have written some code, which is not working at the proper cell address: 我写了一些代码,这些代码在正确的单元地址上不起作用:
function formatForHoliday() {
Logger.clear();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var holidays = ss.getRange("HolidayList!A2:A80").getValues();
// Logger.log(holidays);
// For each sheet
for ( var s = 0; s <= 12; s++ ) {
var sheet = ss.getSheets()[s];
// Logger.log(sheet);
var values = sheet.getRange(sheet.getName() + "!F3:NF3").getValues();
// var newValues = [];
// Logger.log(values);
// In each sheet, for each value
for (i in values[0]) {
for (j in holidays) {
var val = values[0][i];
var hol = holidays[j][0];
Logger.log(val.valueOf());
Logger.log(hol.valueOf());
if (val.valueOf() == hol.valueOf()) {
// newValues.push(values[0][i]);
var cellsRange = sheet.getRange(4, i + 6, 52, 1);
var address = cellsRange.getA1Notation();
cellsRange.setBackground('grey');
}
}
}
}
}
Your code seems to be fine, the only issue I can see right now is that you're fetching range of set[multiple] of cells but setting background to only one. 您的代码似乎很好,我现在唯一能看到的问题是,您正在获取set [multiple]单元格的范围,但将背景设置为一个。
And you're applying setBackground() to a string. 然后将setBackground()应用于字符串。 getA1Notation() returns string not range.
getA1Notation()返回字符串,而不是范围。 https://developers.google.com/apps-script/reference/spreadsheet/range#geta1notation
https://developers.google.com/apps-script/reference/spreadsheet/range#geta1注释
setBackground() can be applied to range only. setBackground()只能应用于范围。
https://developers.google.com/apps-script/reference/spreadsheet/range#setbackgroundcolor https://developers.google.com/apps-script/reference/spreadsheet/range#setbackgroundcolor
var cellsRange = sheet.getRange(4, i + 6, 52, 1);
var address = cellsRange.getA1Notation();
cellsRange.setBackground('grey');
Rather you should set to all of those cells whose range/address you have fetched. 而是应将所有设置其范围/地址已获取的单元格设置为。 https://developers.google.com/apps-script/reference/spreadsheet/range#setbackgroundscolor
https://developers.google.com/apps-script/reference/spreadsheet/range#setbackgroundscolor
I think what you should do is that : 我认为您应该做的是:
var cellsRange = sheet.getRange(4, i + 6, 52, 1);
Now you should make an array of same dimension as of (4, i + 6, 52, 1) ie I think [48X1] 现在,您应该制作与(4,i + 6,52,1 )相同维的数组,即我认为[48X1]
Fill all it's element with 'grey' 用“灰色”填充所有元素
Use setBackgrounds() method. 使用setBackgrounds()方法。 https://developers.google.com/apps-script/reference/spreadsheet/range#setbackgroundscolor
https://developers.google.com/apps-script/reference/spreadsheet/range#setbackgroundscolor
Let me know if this resolves your issue. 让我知道这是否可以解决您的问题。 Or if you have any further doubts/suggestions.
或者,如果您还有其他疑问/建议。
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.