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!
(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:
JavaScript:
You don't actually have to load the range if you just want to set the values, so you can do this: