javascript - 脚本:以粗体设置最大值
问题描述
我在 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
于是我开始思考:
var Item1High = max(b1;d1);
For each row (X1) if value == Item1High then set to bold
但是在这种状态下,最困难的部分是只用粗体设置最正确的值(所以行的最高值)而且我被困在这种状态。
你能帮我吗?我在 JavaScript 中使用 Google Sheet Script。
谢谢 !
解决方案
- 您希望将粗体类型设置为 Google 电子表格每一行中的最大值。
- 例如,当列“B”和“C”是相同的值和最大值时,您希望将粗体设置为“C”列。
- 您想使用 Google Apps 脚本实现此目的。
该示例脚本的流程如下。
流动:
- 从工作表中检索所有值。
- 创建文本样式。
- 设置创建的文本样式。
示例脚本:
在运行脚本之前,请设置工作表名称。
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);
}
推荐阅读
- r - 使用 selectInput 来确定在 r shiny 中应用什么函数
- multithreading - 多线程一个Mongo查找操作
- django - /guardian/guardianhomepage/ 上的 NoReverseMatch
- c++ - 我的 makefile 无法编译并且说什么都不做
- r - 不等的因子水平:强制到字符绑定字符和因子向量
- php - 无法在kali linux上安装php7.2-gd
- c# - 如何将 JSON 转换为 IPoint 类型?
- angular - ChangeDetectorRef 上的手动 TypeScript Angular 依赖注入失败
- java - 无法让扫描仪从文件中读取
- javascript - 使用对象而不是回调函数时过滤函数如何工作?