I'm working on a macro in OfficeScript.
Among other things I need it to check if a column header exists in the given table and add a column if it doesn't exist. This part doesn't seem to work.
Here is the relevant snippet
function QAcolorTable(table: ExcelScript.Table){
console.log("table: " + table.getName());
//find or create column to for score formula
let columnScore = table.getColumn("Score");
console.log('null '+ (columnScore==null));
console.log('type of ' + (typeof columnScore));
console.log("undefined " + (columnScore==undefined));
console.log("not " + !columnScore);
console.log("void " + (columnScore == void 0));
if (!columnScore) {
console.log("adding 'Score'");
table.addColumn(-1, null, "Score");
columnScore = table.getColumn("Score");
}
}
The office script documentation says that if getColumn should return 'undefined' if a column is not found, so I also tried number of tests for an undefined variable, but all return false.
https://learn.microsoft.com/en-us/ja...lumn-member(1)
So at this point I am thinking I may need to add a function to loop through all the headers, but that feels like overkill.
Any suggestions?
Here is my full code. I attached a sample workbook with appropriately named worksheet and table.
function main(workbook: ExcelScript.Workbook) {
let sheets = workbook.getWorksheets();
sheets.forEach(sheet => {
let sheetName = sheet.getName();
if(sheetName.search("QA Checklist") > -1){
QA_color_Sheet(sheet);
}
});
}
function QA_color_Sheet(sheet : ExcelScript.Worksheet){
console.log("sheet: " + sheet.getName() );
let tables = sheet.getTables();
tables.forEach(table => {
let tableName = table.getName();
if(tableName.search("QAChecklist") > -1) {
QAcolorTable(table);
}
});
}
function QAcolorTable(table: ExcelScript.Table){
console.log("table: " + table.getName());
const scoreFormula = "=IF( [@[Consultant Checked]]=\"N/A\",NA(),SWITCH([@[Get Colour]],38, 1, 36, 0.5, 35, 0, \"#C6EFCE\", 0, \"#FFEB9C\", 0.5, \"#FFC7CE\", 1, 1))";
let headers = table.getHeaderRowRange();
//get the column object to read colours from Exit if not found.
let columnRead = table.getColumn("Initial Reviewer Comments");
if (!columnRead) { columnRead = table.getColumn("Reviewer Comments"); }
if (!columnRead) {
console.log("column [Initial Reviewer Comments]||[Initial Reviewer Comments] not found in " + table.getName());
return;
}
let rngRead = columnRead.getRangeBetweenHeaderAndTotal();
//find or create colum to write RGB color to.
let columnWrite = table.getColumn("Get Colour");
if (!columnWrite){
console.log("Adding 'Get Colour'");
table.addColumn(-1, null, "Get Colour");
columnWrite = table.getColumn("Get Colour");
}
let rngWrite = columnWrite.getRangeBetweenHeaderAndTotal();
//find or create column to for score formula
let columnScore = table.getColumn("Score");
console.log('null '+ (columnScore==null));
console.log('type of ' + (typeof columnScore));
console.log("undefined " + (columnScore==undefined));
console.log("not " + !columnScore);
console.log("void " + (columnScore == void 0));
if (!columnScore) {
console.log("adding 'Score'");
table.addColumn(-1, null, "Score");
columnScore = table.getColumn("Score");
}
console.log(scoreFormula)
columnScore.getRangeBetweenHeaderAndTotal().getCell(0,0).setFormula(scoreFormula);
let rows = table.getRowCount();
for (let rw = 0; rw < rows; rw++) {
let cellColorRead = rngRead.getCell(rw, 0);
let cellWrite = rngWrite.getCell(rw, 0);
cellWrite.setValue(cellColorRead.getFormat().getFill().getColor());
}
}
Bookmarks