I'm having trouble fetching the names of sheets from an external Excel file, as I keep getting an empty array.
async function retrieveSheetNames() {
const fileInput = <HTMLInputElement>document.getElementById("file");
const fileReader = new FileReader();
fileReader.onload = (event) => {
Excel.run(async (context) => {
// Remove the metadata before the base64-encoded string.
const startIndex = fileReader.result.toString().indexOf("base64,");
const workbookContent = fileReader.result.toString().substr(startIndex + 7);
// Retrieve the workbook.
const workbook = context.workbook;
// Set up the options for inserting sheets.
var options = {
sheetNamesToInsert: [], // Insert all sheets from the workbook.
positionType: Excel.WorksheetPositionType.after, // Insert after the `relativeTo` sheet.
relativeTo: "Sheet1"
}; // The sheet relative to which the other worksheets will be inserted. Used with `positionType`.
// Insert the workbook.
//workbook.insertWorksheetsFromBase64(workbookContent, options);
var sheetNames = context.workbook.names;
sheetNames.load();
await context.sync();
console.log(JSON.stringify(workbook.names))
return context.sync();
});
};
// Read the file as a data URL to extract the base64-encoded string.
fileReader.readAsDataURL(fileInput.files[0]);
}
This code is adapted from an Office JS sample. Do you have any suggestions on how to successfully retrieve the sheet names?