我想问一下,如果单元格列被涂成另一个工作表,是否有一种方法可以保留颜色(列J)。代码似乎可以工作,但我丢失了J列(红色)的颜色格式。
下面是我使用的代码:
function Extract(){
var ss = SpreadsheetApp.openById('SHEETID');
var sheet = ss.getSheetByName('NAME');
var lr = sheet.getLastRow();
var lc = sheet.getLastColumn();
var sheet1 = sheet.getRange(2,1,lr,lc).getValues();
var sheetBG = sheet.getRange(2,10,lr,1).getBackgrounds();
var newSheet = ss.getSheetByName('NEWSHEET');
var columnJ = "10";
var red = '#f4cccc';
var data = [];
for (var i = 0; i <= lr - 1; i++) {
if (sheetBG[i] == red ) {
data.push(sheet1[i]);
}}
newSheet.getRange(2,1,data.length,17).setValues(data);
}
与获取背景颜色的方法相同您可以在新工作表中使用setbackground
来设置它们newSheet.getRange(2,1,data.length,17).setValues(data);
newSheet.getRange(2,10,sheetBG.length,sheetBG[0].length).setBackgrounds(sheetBG); // new code
如果你只想复制红色的值,那么这样做:
var new_sheetBG=sheetBG.map(r=>r[0]==red?red:"#ffffff") // keep only red and make white the others
newSheet.getRange(2,1,data.length,17).setValues(data);
newSheet.getRange(2,10,new_sheetBG.length,new_sheetBG[0].length).setBackgrounds(new_sheetBG); // new code
function Extract(){
var ss = SpreadsheetApp.openById('SHEETID');
var sheet = ss.getSheetByName('NAME');
var lr = sheet.getLastRow();
var lc = sheet.getLastColumn();
var sheet1 = sheet.getRange(2,1,lr,lc).getValues();
var sheetBG = sheet.getRange(2,10,lr,2).getBackgrounds();
var newSheet = ss.getSheetByName('NEWSHEET');
var red = '#f4cccc';
var data = [];
var color = [];
for (var i = 0; i <= lr - 1; i++) {
var columnJ = sheetBG10[i][0];
var columnK = sheetBG10[i][1]
if (columnJ == red && columnK == red) {
data.push(sheetAR[i]);
color.push(sheetBG10[i]);
}
if (columnJ != red && columnK == red) {
data.push(sheet[i]);
color.push(sheetBG[i]);
}
if (columnJ != red && columnK == red) {
data.push(sheet[i]);
color.push(sheetBG[i]);
}
}
newSheet.getRange(2,1,data.length,data[0].length).setValues(data);
newSheet.getRange(2,10,color.length,color[0].length).setBackgrounds(color);
}