简体   繁体   English

基于不同工作表中的值的单元格颜色范围

[英]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 : 我认为您应该做的是:

  1. You're doing step one ie fetching range correctly. 您正在执行第一步,即正确获取范围。

var cellsRange = sheet.getRange(4, i + 6, 52, 1);

  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]

  2. Fill all it's element with 'grey' 用“灰色”填充所有元素

  3. 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.

相关问题 Google Sheet Script - 根据包含电子邮件的一系列单元格隐藏选项卡 - Google Sheet Script - Hiding tabs based on a range of cells containing emails 我正在寻找将颜色突出显示的单元格移动到不同的工作表 - I'm looking to move cells with color highlighted to different sheet 如何导入没有特定单元格的图纸范围? - How to import a range of a sheet without specific cells? 如何将两个提交的值与谷歌表格单元格匹配并基于此返回 html 模板 - How to match two submitted values with google sheet cells and return html templates based on that 将表单上多个单元格的值复制提交到其他Google表格中的单个单元格中 - Copy values of multiple cells on form submit into a single cell in a different Google Sheet 使用google maps api根据数据中的值使用不同颜色的圆圈 - circles with different color based on the values in the data using google maps api 颜色范围 - 表格中的角度值 - Color range - table values in angular 当基础颜色不同时,基于颜色的不同 - Color based different when underlying color is different jqPlot-基于串联的每个数据点的值范围的不同颜色条 - jqPlot - Different color bars based on value range for each data point in series 根据数据库中的值更改单元格的颜色 - Change color of cells based on value in DB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM