1

I am trying to read the values of range B2 and write an if statement dependent on what is returned. I keep getting an error and I don't know why. Please see below.

function analyzeWorkbook() {
    // Run a batch operation against the Excel object model
    Excel.run(function (context) {
        // Create a proxy object for the selected range and load its properties
        var Sheet = context.workbook.worksheets.getActiveWorksheet();
        var Range = Sheet.getRange("B2");
        Range.load("values");
        // Run the queued-up command, and return a promise to indicate task completion
        return context.sync();

        if (Range.values[0][0] === "Business Unit:") {
            Sheet.getRange("B3").values = "You did it!";
        } else {
            Sheet.getRange("B3").values = "Youre still a rockstar!";
        }

    })
        .catch(errorHandler);
}
  • What error did you meet? I have tried your code, I haven't found any error. here is my gist: https://gist.github.com/lumine2008/1273a84c4ebb9bfe2dc4fc5c7af50bd5 – Raymond Lu Apr 01 '20 at 01:32
  • This is the error message Error PropertyNotLoaded: The property 'values' 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. – Amanda Legere Apr 01 '20 at 02:24
  • I solved it. I opened .then(function () after the first return context.sync() Thanks for your help! – Amanda Legere Apr 01 '20 at 03:08

1 Answers1

0
function analyzeWorkbook() {

    // Run a batch operation against the Excel object model
    Excel.run(function (context) {
        // Create a proxy object for the selected range and load its properties
        var Sheet = context.workbook.worksheets.getActiveWorksheet();
        var Range = Sheet.getRange("B2");
        Range.load("values");
        // Run the queued-up command, and return a promise to indicate task completion
        return context.sync()
            .then(function () {
                if (Range.values[0][0] === "Business Unit:") {
                    Sheet.getRange("B3").values = "You did it!"
                } else {
                    Sheet.getRange("B3").values = "Youre still a rockstar!"
                }
                return context.sync();
            });

    });

}