Как прочитать свойства диапазона в Office. js? - PullRequest
1 голос
/ 01 апреля 2020

Я пытаюсь прочитать значения диапазона B2 и написать оператор if в зависимости от того, что возвращается. Я получаю сообщение об ошибке и не знаю почему. Пожалуйста, смотрите ниже.

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);
}

1 Ответ

0 голосов
/ 01 апреля 2020
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();
            });

    });

}
...