javascriptexceltypescriptexcel-addins

Insert a web-hosted Excel file into current workbook by using Excel JavaScript API


I try to insert an external Excel workbook (including all related worksheets). The file is accessible by url like https://www.excel.com/template.xlsx

However I'm stuck right now. I don't find any documentation how I could achieve this. The only doc I've found is about using the file picker: https://learn.microsoft.com/en-us/javascript/api/excel/excel.workbook?view=excel-js-preview#excel-excel-workbook-insertworksheetsfrombase64-member(1)

This works but that's not the solution I need. I changed the code, that the script downloads the file as blob creates a new file and use the recommended FileReader. But still no success. This is my code so far:

export async function downloadAndImportTemplate() {
const templateFileUrl: string = "https://www.test.com/template.xlsx";
try {
  // Fetch the workbook from server
  const response = await fetch(templateFileUrl);
  const workbookBlob = await response.blob();

// Create a File object
const fileName = "template.xlsx"; 
const excelFile: File = new File([workbookBlob], fileName, { type: workbookBlob.type });

// Use FileReader to read the fetched workbook
const templateFileReader: FileReader = new FileReader();
templateFileReader.onload = (event) => {
  Excel.run((context) => {
    // Remove the metadata before the base64-encoded string.
    let startIndex: number = templateFileReader.result.toString().indexOf("base64,");
    const externalWorkbook: string = templateFileReader.result.toString().substring(startIndex + 7);

    // Use Excel JS API to insert the workbook content
    const currentWorkbook = context.workbook;
    currentWorkbook.insertWorksheetsFromBase64(externalWorkbook);
    return context.sync();
  });
};

templateFileReader.readAsArrayBuffer(excelFile);
} catch (error) {
textError.textContent = error;
}
}

Thanks for any advice


Solution

  • Ok got it. Missed it to read the data as data url. That's my working solution now:

    export async function downloadAndImportTemplate() {
    const templateFileUrl: string = "https://test.com/template.xlsx";
    
    try {
    // Fetch the workbook from your server
    const response = await fetch(templateFileUrl);
    const workbookBlob = await response.blob();
    // Create a File object
    const fileName = "template.xlsx"; // Provide a filename for the File object
    const excelFile: File = new File([workbookBlob], fileName, { type: workbookBlob.type });
    
    // Use FileReader to read the fetched workbook
    const templateFileReader: FileReader = new FileReader();
    templateFileReader.onload = (event) => {
      Excel.run((context) => {
        // Remove the metadata before the base64-encoded string.
        let startIndex: number = templateFileReader.result.toString().indexOf("base64,");
        const templateWorkbook: string = templateFileReader.result.toString().substring(startIndex + 7);
    
        // Set up the insert options.
        let options = {
          sheetNamesToInsert: [], // Insert all the worksheets from the source workbook.
          positionType: Excel.WorksheetPositionType.end, // Insert after the `relativeTo` sheet.
    
        // Use Excel JS API to insert the workbook content
        const currentWorkbook = context.workbook;
        currentWorkbook.insertWorksheetsFromBase64(templateWorkbook, options);
        return context.sync();
      });
    };
    templateFileReader.readAsDataURL(excelFile);
    } catch (error) {
    textError.textContent = error;
    }
    }