使用 Excel JavaScript API 将 Web 承载的 Excel 文件插入当前工作簿

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

提问人:TRS 提问时间:10/11/2023 更新时间:10/12/2023 访问量:28

问:

我尝试插入外部Excel工作簿(包括所有相关工作表)。该文件可通过类似 https://www.excel.com/template.xlsx 的 url 访问

但是,我现在被困住了。我没有找到任何文档如何实现这一目标。我找到的唯一文档是关于使用文件选择器的:https://learn.microsoft.com/en-us/javascript/api/excel/excel.workbook?view=excel-js-preview#excel-excel-workbook-insertworksheetsfrombase64-member(1)

这有效,但这不是我需要的解决方案。我更改了代码,即脚本将文件下载为 blob 创建一个新文件并使用推荐的 FileReader。但仍然没有成功。这是我到目前为止的代码:

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;
}
}

感谢您的任何建议

JavaScript TypeScript Excel-Addins

评论

0赞 Ryan Wilson 10/11/2023
也许这会有所帮助:update-worksheet-from-base64-file-excel-javascript-api

答:

0赞 TRS 10/12/2023 #1

好的,知道了。错过了将数据读取为数据 url 的机会。这就是我现在的工作解决方案:

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;
}
}