I’m trying to use the Excel Javascript APi to sort text based on Strikethrough. What I would like to do is detect all strikethrough text in the range & set to say Red then sort.This is what I have but getting error..
ebugInfo: Object
code: "PropertyNotLoaded"
message: "The property ‘strikethrough’ is not available. Before reading the property’s value, call the load method on the containing object and call "context.sync()" on the associated request context."
$("#run").click(() => tryCatch(run));
async function run() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
// Add values to the range
const range = sheet.getRange("A5:M500");
let dataRange = sheet.getRange("A5:M500").load(["rowCount", "format", "values","font","strikethrough"]);
console.log(dataRange.toJSON());
await context.sync();
for (var row = 0; row < dataRange.rowCount; row++) {
dataRange.getRow(row).format.fill.clear();
if (dataRange.getCell(row, 0).format.font.strikethrough == true) {
dataRange.getRow(row).format.font.color = "Red";
}
}
// Sort the range
const sortFields = [
{
key: 1,
sortOn: Excel.SortOn.fontColor,
ascending: false
},
{
key: 0,
ascending: true
}
];
range.sort.apply(sortFields);
await context.sync();
});
}
/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
try {
await callback();
} catch (error) {
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
console.error(error);
}
}
2
Answers
You are not using the
load()
method correctly. Try replacing:with
Also, see load() for how to use the method.