How do I copy both the values and background color of a filtered range in Google Sheets App Script?

Jorge O

The goal of this script is to basically filter out a specific range based on cell value and copy it onto another sheet, carrying over both the cell value and the cell background colors. I have a script that filters by cell value, and copies the filtered values onto another sheet, which is what I've included in this post, but I can't figure out how to change this to also carry over the cell color for this range.

A copy of the sheet I'm working with https://docs.google.com/spreadsheets/d/1rCXkqNLN2EI97Lk5OTOjxFHloehO7lnIXdioQMhEeks/edit?usp=sharing

const ss = SpreadsheetApp.getActiveSpreadsheet();
const spreadsheet = SpreadsheetApp.getActive();
const sheet = ss.getSheetByName("places");
const rng = sheet.getRange(2,1,sheet.getLastRow(),33).getValues();

function filterAlternatives(){

  let Criteria1 = "Chicago";
  let Criteria2 = "Alternatives";
  let Criteria3 = "Approved";
  let Criteria4 = "Field Check";

  let FData = rng.filter(function(e){
    return e[7] == Criteria1
    && e[19] == Criteria2
    && (e[15] == Criteria3 || e[15] == Criteria4)
  })

  let newSheet = ss.insertSheet(Criteria2);
  newSheet.getRange(2,1,FData.length,33)
    .setValues(FData)
    .setHorizontalAlignment("left")
    .setWrapStrategy(SpreadsheetApp.WrapStrategy.CLIP);
  
  let headers = sheet.getRange("A1:AG1").getValues();
  newSheet.getRange("A1:AG1")
    .setValues(headers)
    .setHorizontalAlignment("left")
    .setWrapStrategy(SpreadsheetApp.WrapStrategy.CLIP);
  
  newSheet.sort(29,false);
  newSheet.setRowHeightsForced(1,FData.length+1,23);
  newSheet.setTabColor("ff0000");
  newSheet.setColumnWidths(1,33,100);
  newSheet.setColumnWidth(1,60);
  newSheet.setColumnWidth(2,300);
  newSheet.setColumnWidth(3,50);
  newSheet.setColumnWidth(4,200);
  newSheet.setColumnWidth(5,50);
  newSheet.setColumnWidth(6,50);
  newSheet.setColumnWidth(13,175);
  newSheet.setColumnWidth(14,175);
  newSheet.setColumnWidth(22,50);
  newSheet.setFrozenRows(1);
  newSheet.setFrozenColumns(2);
}

TheWizEd

I haven't tested it but I believe it should work.

Change your filter to also collect the background colors. Then add the backgrounds to the newsheet.

let backGrounds =[];
let FData = rng.filter(function(e,i){
  if( ( e[7] == Criteria1 )
  &&  ( e[19] == Criteria2 )
  &&  ( ( e[15] == Criteria3 ) || ( e[15] == Criteria4 ) ) {
    backGrounds.push(sheet.getRange(2+i,1,1,33).getBackgrounds()[0]);
    return true;
  }
  return false;
});

newSheet.getRange(2,1,FData.length,33)
  .setValues(FData)
  .setBackgrounds(backGrounds)
  .setHorizontalAlignment("left")
  .setWrapStrategy(SpreadsheetApp.WrapStrategy.CLIP);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to create hyperlink to range in Google Sheets App Script?

How do I copy some specific columns from a given range which is auto filtered in Excel VBA?

How to parse a range in Google Sheets script

How do I sequentially fill a big range of cells in Google Sheets?

How do I expand a range within a dateframe and copy the values?

How do I include the original color when I copy an event on Google Calendar using Apps script?

How do I write a script for that finds the sum of a range in Google Sheets, then divides that by the number of months that have elapsed?

How do I get the true range when editing multiple rows on Google Sheet via Google App Script

How to extract individual array value from filtered row in app script for google sheets

How to copy a range to an array in Google Sheets Apps Script and delete array duplicates?

How can i add list of sheets to Google App Script?

Google Script to watch multiple sheets: how do I simplify this script?

How do I find and delete duplicate values in a range of cells while keeping the first occurrence of a duplicated value in Google Sheets?

How can I filter my rows by background color in Google Sheets?

How do I add Google sheets data vaildation to a column using apps script for cells to only allow colors and hex color codes to be inputted

How to set Background color to a filtered row in Google Sheets using GAS/App Script

How to Copy Filtered Range To Array?

How do I code this google sheets IFS function into app script code?

How do I get the URL for a Google Sheets file that I just created using the copy function - google script

How Do I Exclude Certain Data Types in a Range in Google Sheets?

Google sheets app script pass values as range or individual cells

Google Sheets script to copy a range to another sheet

How do I exclude certain sheets/tabs from google app script?

Google App Script: How do I get only data range from named range?

How do I get values of a column in an array and search for a value in it? (Google Sheets' Apps Script)

How do I change the background color of a cell depending on the date in Google Sheets?

How do I set the range to change automatically in Google Sheets API?

How do I create a button that changes text and color on click in Google Sheets App Scripts / Drawing?

How i use app script to edit google sheets protected range