Skip to content

Instantly share code, notes, and snippets.

@eruffaldi
Created April 6, 2024 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eruffaldi/50d34f2dce3b92a3edc998db5ee79a92 to your computer and use it in GitHub Desktop.
Save eruffaldi/50d34f2dce3b92a3edc998db5ee79a92 to your computer and use it in GitHub Desktop.
Excel Lookup Cell Color
function main(workbook: ExcelScript.Workbook) {
// Specify the sheet names
let mainSheetName = "System Items";
let lookupSheetName = "Foglio1";
// Get the sheets
let mainSheet = workbook.getWorksheet(mainSheetName);
let lookupSheet = workbook.getWorksheet(lookupSheetName);
// Get used ranges (assuming data starts from row 1)
let mainRange = mainSheet.getUsedRange();
let lookupRange = lookupSheet.getUsedRange();
// Sync to populate the values
let mainValues = mainRange.getValues();
let lookupValues = lookupRange.getValues();
// Map to hold value-color pairs from the lookup sheet
let colorMap : Map<String,String> = new Map();
// Populate the map with lookup values and colors
for (let i = 0; i < lookupValues.length; i++) {
colorMap.set(lookupValues[i][0], lookupSheet.getRange(`A${i + 1}`).getFormat().getFill().getColor());
}
// Loop through the main sheet's rows
for (let row = 0; row < mainValues.length; row++) {
let cellValue = mainValues[row][0]; // Column A value
let color = "#FFFFFF";
// Check if the value is in the map
if (colorMap.has(cellValue)) {
// Get the corresponding color
color = colorMap.get(cellValue);
}
// Apply the color to the entire row
mainSheet.getRange(`A${row + 1}:Z${row + 1}`).getFormat().getFill().setColor(color);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment