Javascript 脚本:以粗体设置最高值
我在Google Sheet中得到了一个数组,看起来像这样:Javascript 脚本:以粗体设置最高值,javascript,arrays,google-apps-script,google-sheets,Javascript,Arrays,Google Apps Script,Google Sheets,我在Google Sheet中得到了一个数组,看起来像这样: +-----+---------+------------+------------+------------+-----+ | | A | B | C | D | ... | +-----+---------+------------+------------+------------+-----+ | 0 | | Mission #1 |
+-----+---------+------------+------------+------------+-----+
| | 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)代码>
对于每行(X1),如果值==Item1High,则设置为粗体
- 您希望将粗体类型设置为Google电子表格每行中的最大值
- 例如,当列“B”和“C”是相同的值和最大值时,您希望将粗体设置为列“C”
- 您希望使用谷歌应用程序脚本实现这一点
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);
}
你的理解是正确的,除了一件事:我不想一次分析所有的值,我想分别得到每一行的最大值。所以第1行的最大值不同于第2行。。。etc@Saku谢谢你的回复。从您的回复中,在我的示例脚本中,从每行检索最大值,并在每行设置文本样式。所以我无法理解你的回答。我可以问你关于我的样本脚本问题的细节吗?@Saku作为一个测试用例,当我的脚本针对你的问题中的样本表运行时(标题列和标题行是第1行和第“a”列),对于第2行、第3行和第4行,“D”、“C”和“C”列分别设置为粗体样式。我明白这种情况是你的目标。我的理解正确吗?您的脚本似乎运行良好,但是否可以指定某个范围?因为我在其他单元格中得到了可能与脚本一起出现的字符串?它也很好用,但不是在我的所有阵列上(前7行不受影响)检查屏幕:@Saku谢谢你的回复。我的示例脚本来自您问题中的示例值。对此我深表歉意。从您的回复中,我又添加了一个示例脚本。你能确认一下吗?如果这不是你想要的结果,我再次道歉。