首页 > 解决方案 > 脚本:以粗体设置最大值

问题描述

我在 Google Sheet 中有一个数组,看起来像这样:

+-----+---------+------------+------------+------------+-----+
|     |    A    |      B     |      C     |      D     | ... |
+-----+---------+------------+------------+------------+-----+
|  0  |         | Mission #1 | Mission #2 | Mission #3 | ... |
+-----+---------+------------+------------+------------+-----+
|  1  | Item #1 |     100    |     250    |     250    | ... |
+-----+---------+------------+------------+------------+-----+
|  2  | Item #2 |     200    |    1000    |     500    | ... |
+-----+---------+------------+------------+------------+-----+
|  3  | Item #3 |     300    |    3000    |     800    | ... |
+-----+---------+------------+------------+------------+-----+
| ... |   ...   |     ...    |     ...    |     ...    | ... |
+-----+---------+------------+------------+------------+-----+

我想创建一个脚本,将每个项目的最高值加粗。但是有一个问题,正如你所看到的,有些物品在不同的任务中得到相同的值,我需要的是在这种情况下,只有“最右边”需要加粗。

所以我期望做的是:

+-----+---------+------------+------------+------------+-----+
|     |    A    |      B     |      C     |      D     | ... |
+-----+---------+------------+------------+------------+-----+
|  0  |         | Mission #1 | Mission #2 | Mission #3 | ... |
+-----+---------+------------+------------+------------+-----+
|  1  | Item #1 |     100    |     250    |   **250**  | ... |
+-----+---------+------------+------------+------------+-----+
|  2  | Item #2 |     200    |  **1000**  |     500    | ... |
+-----+---------+------------+------------+------------+-----+
|  3  | Item #3 |     300    |  **3000**  |     800    | ... |
+-----+---------+------------+------------+------------+-----+
| ... |   ...   |     ...    |     ...    |     ...    | ... |
+-----+---------+------------+------------+------------+-----+
** = is bold

于是我开始思考:

但是在这种状态下,最困难的部分是只用粗体设置最正确的值(所以行的最高值)而且我被困在这种状态。

你能帮我吗?我在 JavaScript 中使用 Google Sheet Script。

谢谢 !

标签: javascriptarraysgoogle-apps-scriptgoogle-sheets

解决方案


  • 您希望将粗体类型设置为 Google 电子表格每一行中的最大值。
  • 例如,当列“B”和“C”是相同的值和最大值时,您希望将粗体设置为“C”列。
  • 您想使用 Google Apps 脚本实现此目的。

该示例脚本的流程如下。

流动:

  1. 从工作表中检索所有值。
  2. 创建文本样式。
  3. 设置创建的文本样式。

示例脚本:

在运行脚本之前,请设置工作表名称。

function myFunction() {
  const sheetName = "Sheet1";

  // 1. Retrieve all values from the sheet.
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  const range = sheet.getDataRange();
  const values = range.getValues();
  values.shift();

  // 2. Create text styles.
  const tempStyle = SpreadsheetApp.newTextStyle().setBold(false).build();
  const bold = SpreadsheetApp.newTextStyle().setBold(true).build();
  const styles = values.map(r => {
    r.shift();
    let ar = Array(r.length).fill(tempStyle);
    ar[r.lastIndexOf(Math.max(...r))] = bold;
    return ar;
  });

  // 3. Set the created text styles.
  range.offset(1, 1, styles.length, styles[0].length).setTextStyles(styles);
}

参考:

添加:

在此示例脚本中,使用了选定的范围。

function myFunction() {
  const sheetName = "Sheet1";
  const selectRange = "B2:D4";  // Please set the range you want to use.

  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  const tempStyle = SpreadsheetApp.newTextStyle().setBold(false).build();
  const bold = SpreadsheetApp.newTextStyle().setBold(true).build();
  const range = sheet.getRange(selectRange);
  const values = range.getValues();
  const styles = values.map(r => {
    let ar = Array(r.length).fill(tempStyle);
    ar[r.lastIndexOf(Math.max(...r))] = bold;
    return ar;
  });
  range.setTextStyles(styles);
}

推荐阅读