Excel office-js API - Insert values to a blank sheet

假如想象 提交于 2020-01-06 05:26:26

问题


How can I insert values into a blank sheet?
The way that I currently know of for inserting values is by retrieving the range and then inserting the values using the values property, such as

range.getRange("A1:" + cellBottomRight);
range.load("values");
context.sync().then(function () {
    range.values = twoDimensionalArrayOfValues;
});

Is there a simpler way by using a single function to simply insert the values, rather than first retrieving the range?

Thanks!

EDIT:
I've been trying to create a new sheet and then insert a 2-dimensional array to it, which its values would be inserted starting from the cell A1.
So far, I managed to do the following:

let neeSheet = context.workbook.worksheets.add("New sheet");
newSheet.activate();
newSheet.getRange("A1").values = twoDimensionalArray;
context.sync();

But didn't work.

How can I get it to work?
Thanks!


回答1:


(Adding this answer in response to the new information that @avi12 added to the question above, under "EDIT")

Philip's answer above correctly shows how to insert a single value into cell A1 of a worksheet. To address the specific scenario that you've described in your (updated) question, here are some code snippets (one in TypeScript and the other in JavaScript) that show how to create a new worksheet and then add data to the worksheet, using a 2-dimensional array of data. The key thing to point out here is that I'm retrieving the range by using getResizedRange (passing in dimensions of my array), so that the size of the range matches the size of the data set that I'm inserting into it.

Note: You can quickly and easily try these snippets yourself by using Script Lab (https://aka.ms/getscriptlab). Simply install the Script Lab add-in (free), then choose "Import" in the navigation menu, and use the following GIST URL: https://gist.github.com/kbrandl/01c4faf352c34286188311c1198f6307.

TypeScript:

async function run_TS() {
    try {
        await Excel.run(async (context) => {

            // define values that will be inserted into new sheet
            let values = [["A1", "B1", "C1"], ["A2", "B2", "C2"]]; 

            // create and activate new sheet
            let sheets = context.workbook.worksheets;
            let newSheet = sheets.add("New sheet TS");
            newSheet.activate();

            // add data to the new sheet
            let range = newSheet.getRange("A1").getResizedRange(values.length - 1, values[0].length - 1);
            range.values = values;

            // sync
            await context.sync();

            console.log("Finished with run_TS function");
        });
    }
    catch (error) {
        OfficeHelpers.UI.notify(error);
        OfficeHelpers.Utilities.log(error);
    }
}

JavaScript:

function run_JS() {
    Excel.run(function (context) {

        // define values that will be inserted into new sheet
        var values = [["A1", "B1", "C1"], ["A2", "B2", "C2"]];

        // create and activate new sheet
        var sheets = context.workbook.worksheets;
        var newSheet = sheets.add("New sheet JS");
        newSheet.activate();

        // add data to the new sheet
        var range = newSheet.getRange("A1").getResizedRange(values.length - 1, values[0].length - 1);
        range.values = values;

        // sync        
        return context.sync()
            .then(function () {
                console.log("Finished with run_JS function");
            });
    })
        .catch(function (error) {
            OfficeHelpers.UI.notify(error);
            OfficeHelpers.Utilities.log(error);
        });
}



回答2:


You don't actually have to load the range if you just want to set the values, so you can do this:

var range = ctx.workbook.worksheets.getItem("Sheet1").getRange("A1");
range.values = [["Value"]];
return ctx.sync();


来源:https://stackoverflow.com/questions/45680289/excel-office-js-api-insert-values-to-a-blank-sheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!