Estoy escribiendo un complemento de Office para Excel que necesita agregar una hoja de trabajo condicionalmente: cuando no está allí: agréguela y complétela. Cuando esté allí: llénalo.
Ahora veo estas API :
En ambos casos, terminaría con una hoja con el nombre de 'Muestra'. Pero, ¿cómo sé cuál elegir?
La solución cruda será intentar obtener la hoja primero y luego, cuando eso falle (todo asíncrono en la llamada .sync ()), agregue la hoja:
async function checkedAddSheet() { Excel.run(async (context) => { // For debugging: OfficeExtension.config.extendedErrorLogging = true; var sheet = context.workbook.worksheets.getItem(NEW_DATA_SHEET_NAME); sheet.load("name, position"); return context.sync() .then(function () { console.log(`Found worksheet named "${sheet.name}" in position ${sheet.position}`); }); }).then(function () { console.log("Done"); }).catch(function (error) { console.error(error); if (error instanceof OfficeExtension.Error) { console.log("Debug info: " + JSON.stringify(error.debugInfo)); } console.log("Failed to get, let's add..."); addSheet(); }); } function addSheet() { Excel.run(async (context) => { var sheet = context.workbook.worksheets.add(NEW_DATA_SHEET_NAME); sheet.load("name, position"); return context.sync() .then(function () { console.log(`Worksheet named "${sheet.name}" was added in position ${sheet.position}`); }); }).then(function () { console.log("Done"); }).catch(function (error) { console.error(error); if (error instanceof OfficeExtension.Error) { console.log("Debug info: " + JSON.stringify(error.debugInfo)); } console.log("Failed to get, let's add..."); }); }Esto podría mejorarse comprobando el tipo de error, pero funciona.