Array.push.setAnyFormatting ('red')?
Descrição:
O usuário do Stack Overflow mhawksey fez recentemente algumasotimização fantástica do meu código e, ao fazê-lo, apresentou-me a impulsos de matriz super eficientes. Mas trabalhar com matrizes é meio difícil, porque parece que não consigo usar funções que posso ao usar a abordagem tradicional .getRange / .setValue.
Problema:
Preciso integrar .setFontColors ('red') e .setBackgroundColors ('white').
Código e Imagens:
Primeiro, postarei o código. Segundo, uma imagem do que oatualmente código faz. Terceiro, uma imagem do que o códigonecessidades façam.
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);
}
Isto é o que o código faz:
Isto é o que estou tentando alcançar:
Atualizar:
Anexei um loop de formatação simples e funcional. O problema é que, quando eu o executo em uma coluna de dados mais longa, leva muito tempo para processar. Pelo que entendi dos comentários, não consigo formatar rapidamente uma planilha. Estou errado?
Código de formatação anexado:
//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');
};
};