(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);
});
}
context.sync();, and I also triedreturn context.sync();