1

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!

4
  • To elaborate some on Philip's answer below: OBJECT.LOAD(PROPERTIES) is required in situations where you plan to subsequently read those property values. If you're only planning to set the object's properties (as you are, in the scenario that you've described above), or to call methods on the object, or to use the object to navigate to another object, you do not need to call OBJECT.LOAD(PROPERTIES). Commented Aug 14, 2017 at 23:29
  • I'm basically trying to create a new sheet and insert there a values that I have in a 2-dimensional array. The code below did work, but only for a string - it didn't allow me to insert a 1-dimensional or a 2-dimensional array to the range. Of course, I have at the end of the code the context.sync();, and I also tried return context.sync(); Commented Aug 16, 2017 at 14:17
  • Updated the main post. Commented Aug 16, 2017 at 17:31
  • Please see the new answer that I've added below. Commented Aug 16, 2017 at 18:52

2 Answers 2

4

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();
Sign up to request clarification or add additional context in comments.

1 Comment

What about inserting values to a newly created sheet? When I tried to do var newRange = context.workbook.worksheets.add("New sheet");, then newRange.activate(); and finally return context.sync().then(function () { newRange.getRange("A1").values = twoDimensionalArrayOfValues; }); - but that doesn't work
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.