转换为AppScript:根据一天中的日期和时间的条件格式

发布于 2025-01-23 05:55:20 字数 514 浏览 0 评论 0原文

我正在为Gsheets的大学做学习策划师。我读到,在Gsheets的内置条件格式中,细胞边框无法格式化。这就是为什么我想转换我的con。格式化为应用程序脚本,但是我只知道非常基本的应用程序脚本对我来说很难。

这三个条件格式规则(应用于列g& h,i& j&k& l分别):

 =AND($D2=TODAY(); MOD(NOW();1)<TIME(12;0;0))

 =AND($D2=TODAY(); MOD(NOW();1)>TIME(12;0;0);MOD(NOW();1)<TIME(18;0;0))

 =AND($D2=TODAY(); MOD(NOW();1)>TIME(18;0;0))

d列包含日期,并且该规则颜色cellis g&amp; h如果是在下午12点之前,颜色列i&amp;下午12点和下午6点,以及颜色列K&amp; l如果是下午6点。

正如我还想为边框上色(没有应用程序脚本的情况下,这是不可能的),有人可以帮助我实现此应用程序脚本吗?

I am making a study planner for university in Gsheets. I read that in Gsheets' inbuilt Conditional Formatting, the cell border cannot be formatted. That's why I want to convert my cond. formatting to Apps Script, however this particular case is difficult to me as I only know very basic Apps Script.

The three Conditional Formatting rules are (applied to columns G&H, I&J and K&L respectively):

 =AND($D2=TODAY(); MOD(NOW();1)<TIME(12;0;0))

 =AND($D2=TODAY(); MOD(NOW();1)>TIME(12;0;0);MOD(NOW();1)<TIME(18;0;0))

 =AND($D2=TODAY(); MOD(NOW();1)>TIME(18;0;0))

Column D contains dates, and this rule colors cells G&H if it's before 12pm, colors columns I&J if it's between 12pm and 6pm, and colors columns K&L if it is past 6pm.

As I want to color the borders also, which is not possible without Apps Script, could someone help me to implement this is Apps Script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

香草可樂 2025-01-30 05:55:20

据我了解,您创建的公式已经返回truefalse值,对吗?

然后,根据其所包含的价值,将样式应用于它们非常容易。您只需要浏览这些值,检查它们是否包含truefalse,然后根据该条件应用样式。

  1. 通过Extensions&gt;应用脚本
  2. 复制此脚本:
function setBorderCells() {
  /* SELECT THE RANGE TO EVALUATE */
  const rangeOfDates = sS.getRange('G:L').getValues().filter(n => n[0] !== '')
  for (let i = 0; i < rangeOfDates.length; ++i) {
    for (let k = 0; k < rangeOfDates[i].length; k++) {
      /* AS THE VALUES STARTS IN THE G COLUMN WE START FROM (1,7) EQUAL TO G1 */
      const cell = sS.getRange(1 + i, 7 + k)
      if (cell.getValue()) {
        /* APPLY STYLES IF THE CONDITION IS TRUE */
        cell.setBorder(true, true, true, true, false, false, "green", SpreadsheetApp.BorderStyle.SOLID_THICK);
      } else {
        /* APPLY STYLES IF THE CONDITION IS FALSE */
        cell.setBorder(true, true, true, true, false, false, "red", SpreadsheetApp.BorderStyle.SOLID_THICK);
      }
    }
  }
}

从此处,如 @ruben 在评论中提到,您将必须查看应用程序的应用程序脚本文档以应用您想要的样式。

应该澄清的是,这不是最有效的方法,但我认为这是对Google Apps脚本工作方式的很好介绍。要优化此脚本,您应该研究如何使用 >批处理操作 工作并从那里转换脚本。

文档

From what I understand the formulas you have created already return a TRUE and FALSE value, right?

Then it is very easy to apply a style to them depending on the value they contain. You simply have to go through the values, check if they contain TRUE or FALSE, and apply the styles depending on that condition.

  1. Create a new bounded script in your Sheet via Extensions > Apps Script.
  2. Copy this script:
function setBorderCells() {
  /* SELECT THE RANGE TO EVALUATE */
  const rangeOfDates = sS.getRange('G:L').getValues().filter(n => n[0] !== '')
  for (let i = 0; i < rangeOfDates.length; ++i) {
    for (let k = 0; k < rangeOfDates[i].length; k++) {
      /* AS THE VALUES STARTS IN THE G COLUMN WE START FROM (1,7) EQUAL TO G1 */
      const cell = sS.getRange(1 + i, 7 + k)
      if (cell.getValue()) {
        /* APPLY STYLES IF THE CONDITION IS TRUE */
        cell.setBorder(true, true, true, true, false, false, "green", SpreadsheetApp.BorderStyle.SOLID_THICK);
      } else {
        /* APPLY STYLES IF THE CONDITION IS FALSE */
        cell.setBorder(true, true, true, true, false, false, "red", SpreadsheetApp.BorderStyle.SOLID_THICK);
      }
    }
  }
}

From here, as @Ruben mentions in the comment, you will have to review the Apps Script documentation to apply the styles you want.

It should be clarified that this is not the most efficient way to do it, but I think it is a good introduction to how Google Apps Script works. For an optimization of this Script you should study how to use batch operation works and transform the script from there.

Documentation
<逆流佳人身旁 2025-01-30 05:55:20

尝试(根据需要适应条件,颜色和范围的范围)

function setBorderCells() {
  const ss = SpreadsheetApp.getActiveSpreadsheet()
  const sh = ss.getActiveSheet()
  bordersUpdating(ss.getId(), sh.getSheetId(), 2, 6, 7, 8)
}
function bordersUpdating(id, gid, startRow, endRow, startColumn, endColumn) {
  const resource = {
    "requests": [
      {
        "updateBorders": {
          "range": {
            "sheetId": gid,
            "startRowIndex": +startRow - 1,
            "endRowIndex": +endRow,
            "startColumnIndex": +startColumn - 1,
            "endColumnIndex": +endColumn
          },
          "top": {
            "style": "SOLID",
            "width": 1,
            "color": {
              "blue": 1.0
            },
          },
          "bottom": {
            "style": "SOLID",
            "width": 1,
            "color": {
              "blue": 1.0
            },
          },
          "left": {
            "style": "SOLID",
            "width": 1,
            "color": {
              "blue": 1.0
            },
          },
          "right": {
            "style": "SOLID",
            "width": 1,
            "color": {
              "blue": 1.0
            },
          },
          "innerHorizontal": {
            "style": "SOLID",
            "width": 1,
            "color": {
              "blue": 1.0
            },
          },
          "innerVertical": {
            "style": "SOLID",
            "width": 1,
            "color": {
              "blue": 1.0
            },
          },
        }
      }
    ]
  }
  Sheets.Spreadsheets.batchUpdate(resource, id);
}

启用Google Sheets API服务

Try (adapt conditions, colors and limits of range as necessary)

function setBorderCells() {
  const ss = SpreadsheetApp.getActiveSpreadsheet()
  const sh = ss.getActiveSheet()
  bordersUpdating(ss.getId(), sh.getSheetId(), 2, 6, 7, 8)
}
function bordersUpdating(id, gid, startRow, endRow, startColumn, endColumn) {
  const resource = {
    "requests": [
      {
        "updateBorders": {
          "range": {
            "sheetId": gid,
            "startRowIndex": +startRow - 1,
            "endRowIndex": +endRow,
            "startColumnIndex": +startColumn - 1,
            "endColumnIndex": +endColumn
          },
          "top": {
            "style": "SOLID",
            "width": 1,
            "color": {
              "blue": 1.0
            },
          },
          "bottom": {
            "style": "SOLID",
            "width": 1,
            "color": {
              "blue": 1.0
            },
          },
          "left": {
            "style": "SOLID",
            "width": 1,
            "color": {
              "blue": 1.0
            },
          },
          "right": {
            "style": "SOLID",
            "width": 1,
            "color": {
              "blue": 1.0
            },
          },
          "innerHorizontal": {
            "style": "SOLID",
            "width": 1,
            "color": {
              "blue": 1.0
            },
          },
          "innerVertical": {
            "style": "SOLID",
            "width": 1,
            "color": {
              "blue": 1.0
            },
          },
        }
      }
    ]
  }
  Sheets.Spreadsheets.batchUpdate(resource, id);
}

enable google sheets api servide

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文