简体   繁体   English

Google Sheet App Script:将 1 个值从一张纸匹配到另一张纸,然后如果条件满足设置背景

[英]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");
          }
      }
    }   
  }
}

2nd Sheet 'Roster'第二张“花名册” 在此处输入图像描述

1st Sheet 'List'第一张“列表” 在此处输入图像描述

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?在您的情况下,以下示例脚本如何?

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);
}
  • The flow of this script is as follows.该脚本的流程如下。
    1. Retrieve 2 sheets.检索 2 张。
    2. Retrieve values.检索值。
    3. Create an object for searching names.创建一个 object 用于搜索名称。
    4. Create an array for changing the background color of cells.创建一个用于更改单元格背景颜色的数组。
    5. Change the background color of cells.更改单元格的背景颜色。

Testing:测试:

When this script is run to your sample Spreadsheet, the following result is obtained.当此脚本运行到您的示例电子表格时,将获得以下结果。

From:从:

在此处输入图像描述

To:到:

在此处输入图像描述

Note:笔记:

  • 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.所以,请注意这一点。

References:参考:

Added:添加:

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?在这种情况下,下面的示例脚本怎么样?

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.

相关问题 Google 工作表应用程序脚本 - 根据循环条件将数据从一张工作表复制到另一张工作表 - Google sheet app script - Copy data from one sheet to another based on condition with loop 根据条件将单元格值从一个Google工作表复制到另一个 - Copy cell values from one google sheet to another based on condition Google Apps脚本-从另一个工作表导入工作表值 - Google apps script - Importing sheet values from another sheet Google 工作表脚本将数据集从一张工作表复制到另一张工作表的顶部(仅限值) - Google sheets script copy data sets from one sheet to another (values only) at the top of the sheet 使用谷歌应用脚本将谷歌工作表中的数据拆分到另一张工作表 - Split data in google sheet to another sheet using google app script Google表格应用脚本时间戳记条件 - Google Sheet App Script Timestamp Condition(s) 用于匹配和复制基于另一个相邻单元格的 Google 表格脚本 - Google sheet script to match and copy based from another adjacent cell 如何使用 Google 脚本从 1 个 Google 工作表中复制特定值并粘贴到另一个工作表? - How can I copy specific values from 1 Google Sheet and paste to another Sheet using Google Script? 将数据从一张纸复制到另一张 - Google Script - Copy Data from one sheet to another - Google Script 将范围从一个工作表复制到Google工作表中的另一个工作表 - Copy a range from one sheet to another in google sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM