/*
 * Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
 * See LICENSE in the project root for license information.
 */

/* global console, document, Excel, Office */

Office.onReady((info) => {
  if (info.host === Office.HostType.Excel) {
    document.getElementById("sideload-msg").style.display = "none";
    document.getElementById("app-body").style.display = "flex";
    document.getElementById("run").onclick = runExcel;

    Office.actions.associate("SHOWTASKPANE", () => {
      return Office.addin
        .showAsTaskpane()
        .then(() => {
          return;
        })
        .catch((error) => {
          return error.code;
        });
    });

    Office.actions.associate("HIDETASKPANE", () => {
      return Office.addin
        .hide()
        .then(() => {
          return;
        })
        .catch((error) => {
          return error.code;
        });
    });
  }
});

export async function runExcel() {
  try {
    await Excel.run(async (context) => {
      /**
       * Insert your Excel code here
       */
      const range = context.workbook.getSelectedRange();

      // Read the range address
      range.load("address");

      // Update the fill color
      range.format.fill.color = "yellow";

      await context.sync();
      console.log(`The range address was ${range.address}.`);
    });
  } catch (error) {
    console.error(error);
  }
}
