gpt4 book ai didi

javascript - Array.push.setAnyFormatting ('red')?

转载 作者:行者123 更新时间:2023-11-30 12:05:15 25 4
gpt4 key购买 nike

描述:

Stack Overflow 用户 mhawksey 最近做了一些 fantastic optimization我的代码,并在这样做的过程中,向我介绍了超高效的数组推送。但是使用数组有点困难,因为在使用传统的 .getRange/.setValue 方法时,我似乎无法使用我可以使用的函数。

问题:

我需要整合 .setFontColors('red') 和 .setBackgroundColors('white')。

代码和图像:

首先,我将发布代码。第二,当前 代码功能的图像。第三,代码需要做什么的图片。

function format() {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var lastRow = s.getLastRow();
var row;

//gets a [][] of all values in the column
var data = s.getRange("A:A").getValues();
//we are going to build a [][] to output result
var output = [];

//loop through all cells in column A
for (row = 0; row < lastRow; row++) {
var cellValue = data[row][0];
var dash = false;
if (typeof cellValue === 'string') {
dash = cellValue.substring(0, 1);
//if a number copy to our output array
} else {
output.push([cellValue]);
}

//if -dash
if (dash === "-") {
//build first + last name
var name = (data[(row+1)][0]+" "+data[(row+2)][0]).trim();
//add row for the -state (e.g. -MI)
output.push([cellValue]);
output.push([name]);
output.push(["Order complete"]);
//add a blank row
output.push([""]);
//jump an extra row to speed things up
row++;
}
}
//set the values we've made in our output [][] array
s.getRange(1, 1, output.length).setValues(output);
}

这是代码的作用:

before and after

这就是我要实现的目标:

objective with color

更新:

我附加了一个简单的、有效的格式化循环。问题是,当我在较长的数据列上运行它时,处理时间太长。据我对评论的了解,我无法快速格式化电子表格。我错了吗?

附加格式代码:

//other variables
var range1;

//loop through column A
for (var row = 0; row < lastRow; row++) {
range1 = s.getRange(row + 1, 1);

//define offsets for if statement
var offset1 = range1.offset(1, 0);
var offset2 = range1.offset(2, 0);

//substring cannot run on numbers, so...
cellValue = range1.getValue();
if (typeof cellValue === 'number') {continue;};
dash = cellValue.substring(0, 1);

//if -
if (dash === "-") {
offset1.setFontColor('red');
offset2.setBackground('green');
};
};

最佳答案

您可以使用各种电子表格方法获取设置颜色、字体大小、字体粗细等。来自 不同的数组,但您不能将这些“属性”混合在一个项目中。参见 the doc here .

或者更方便的是,在您的脚本编辑器中编写一个脚本来定义一个范围并使用自动完成来查看您可以用它做的所有事情......

(控制+空格键)

enter image description here


根据您的代码更新进行编辑。

您应该创建第二个数组来保存您范围内的所有背景颜色,并根据您的需要填充它。在下面的代码中,我构建了与您的输出数组平行的数组,不是很优雅,而是系统地展示它是如何工作的。

请注意,空值表示“无背景色”,而#0F0 是绿色的十六进制代码,但如果您愿意,也可以使用“绿色”字符串...

    ...
var output = [];
var backGrounds=[]
//loop through all cells in column A
for (row = 0; row < lastRow; row++) {
var cellValue = data[row][0];
var dash = false;
if (typeof cellValue === 'string') {
dash = cellValue.substring(0, 1);
//if a number copy to our output array
} else {
output.push([cellValue]);
backGrounds.push([null]);
}

//if -dash
if (dash === "-") {
//build first + last name
var name = (data[(row+1)][0]+" "+data[(row+2)][0]).trim();
//add row for the -state (e.g. -MI)
output.push([cellValue]);
backGrounds.push([null]);
output.push([name]);
backGrounds.push([null]);
output.push(["Order complete"]);
backGrounds.push(['#0F0']);
//add a blank row
output.push([""]);
backGrounds.push([null]);
//jump an extra row to speed things up
row++;
}
}
s.getRange(1, 1, output.length).setBackgrounds(backGrounds);
...

关于javascript - Array.push.setAnyFormatting ('red')?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35431531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com