[英]Google Sheet App Script: Match 1 values from one sheet to another sheet and then if condition met set background
I'm new to app script and still learning how to do thing.我是应用程序脚本的新手,仍在学习如何做事。 Here's what I want to do: I have 2 sheets, in the 1st are just the names ('List') in columns based on each person language, the 2nd one is schedule ('Roster').这是我想要做的:我有 2 张表,第 1 张只是基于每个人语言的列中的名称(“列表”),第 2 张是时间表(“名册”)。 I want to change cell background into green the cell with person name when person is on schedule from 8-5.我想在 8 点到 5 点准时将带有人名的单元格背景更改为绿色。
Any help or idea is very appreciated.非常感谢任何帮助或想法。 Thank you in advance.先感谢您。
Here's what I tried so far to do:到目前为止,这是我尝试做的事情:
function addShiftColor() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var actSheet = ss.getSheetByName('List');
var rosSheet = ss.getSheetByName('Roster');
var lastCol = actSheet.getLastColumn() +1;
var lastRow = actSheet.getLastRow() +1;
var end = rosSheet.getLastRow();
for (var column = 1; column < lastCol; column++) {
for (var row = 1; row < lastRow; row++) {
var cellget = actSheet.getRange(row, column).getValue();
var cellset = actSheet.getRange(row, column);
for(i=1, j=5; i <= end, j <= end; i++,j++){
var cell1 = rosSheet.getRange(i, 1).getValue();
var cell2 = rosSheet.getRange(j, 253).getValue();
if(cell1 === cellget && cell2 === "08 -- 17"){
cellset.setBackground("green");
}
}
}
}
}
This is the result I want to achieve.这是我想要达到的结果。
Here is the link to the file https://docs.google.com/spreadsheets/d/1wfSEQtqZJ3XEPla_KRBp7v56sPV5wlciHEHA7IS0MaI/edit?usp=sharing这是文件https://docs.google.com/spreadsheets/d/1wfSEQtqZJ3XEPla_KRBp7v56sPV5wlciHEHA7IS0MaI/edit?usp=sharing的链接
Thank you!谢谢!
In your situation, how about the following sample script?在您的情况下,以下示例脚本如何?
function myFunction() {
// 1. Retrieve 2 sheets.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const listSheet = ss.getSheetByName("List");
const rosterSheet = ss.getSheetByName("Roster");
// 2. Retrieve values.
const listRange = listSheet.getRange(2, 1, listSheet.getLastRow() - 1, listSheet.getLastColumn());
const [, , dates, , ...rosterValues] = rosterSheet.getDataRange().getDisplayValues();
// 3. Create an object for searching names.
const today = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "dd MMM");
const col = dates.indexOf(today);
if (col == -1) {
throw new Error(`Date of ${today} was not found.`);
}
const obj = rosterValues.reduce((o, r) => {
if (r[0]) o[r[0]] = r[col];
return o;
}, {});
// 4. Create an array for changing the background color of cells.
const colors = listRange.getValues().map((r, i) => r.map((c, j) => obj[c] && obj[c] == "08 -- 17" ? "green" : null));
// 5. Change the background color of cells.
listRange.setBackgrounds(colors);
}
When this script is run to your sample Spreadsheet, the following result is obtained.当此脚本运行到您的示例电子表格时,将获得以下结果。
In this sample, green
is used as the background color.在此示例中, green
用作背景色。 This is from your script.这是来自您的脚本。 If you want to change this, please modify "green"
.如果你想改变这个,请修改"green"
。
This sample script is for your sample Spreadsheet.此示例脚本适用于您的示例电子表格。 So, when the structure of the Spreadsheet is changed, this script might not be able to be used.因此,当 Spreadsheet 的结构发生变化时,此脚本可能无法使用。 So, please be careful abuot this.所以,请注意这一点。
About your following additional question,关于您的以下附加问题,
I have a question, If I would like to add as well different colors for 11-20 and 17-20, what should be changed in the script?我有一个问题,如果我想为 11-20 和 17-20 添加不同的 colors,脚本中应该更改什么?
In this case, how about the following sample script?在这种情况下,下面的示例脚本怎么样?
function myFunction() {
const colorObj = { "08 -- 17": "green", "17 -- 02": "blue", "11 -- 20": "red" }; // Please modify this for your actual situation.
// 1. Retrieve 2 sheets.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const listSheet = ss.getSheetByName("List");
const rosterSheet = ss.getSheetByName("Roster");
// 2. Retrieve values.
const listRange = listSheet.getRange(2, 1, listSheet.getLastRow() - 1, listSheet.getLastColumn());
const [, , dates, , ...rosterValues] = rosterSheet.getDataRange().getDisplayValues();
// 3. Create an object for searching names.
const today = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "dd MMM");
const col = dates.indexOf(today);
if (col == -1) {
throw new Error(`Date of ${today} was not found.`);
}
const obj = rosterValues.reduce((o, r) => {
if (r[0]) o[r[0]] = r[col];
return o;
}, {});
// 4. Create an array for changing the background color of cells.
const keys = Object.keys(colorObj);
const colors = listRange.getValues().map((r, i) => r.map((c, j) => obj[c] && keys.includes(obj[c]) ? colorObj[obj[c]] : null));
// 5. Change the background color of cells.
listRange.setBackgrounds(colors);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.