Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uploading data into an Excel Spreadsheet. #3036

Closed
hyperliskdev opened this issue May 16, 2024 · 8 comments
Closed

Uploading data into an Excel Spreadsheet. #3036

hyperliskdev opened this issue May 16, 2024 · 8 comments

Comments

@hyperliskdev
Copy link

What version of PnPjs library you are using

3.x

Minor Version Number

3.19.0

Target environment

All

Additional environment details

Currently performing testing locally on the workbench.

Question/Request

I am currently trying to add data into a sheet. I assumed that the function from getFileByServerRelativePath().

await getSP()
    .web.getFileByServerRelativePath(url)
    .getBuffer()
    .then((buffer) => {
      let workbook = new Excel.Workbook();
      workbook.xlsx.load(buffer).then((workbook) => {
        let worksheet = workbook.getWorksheet("sheet_name");

        workbook.xlsx.writeBuffer().then((buffer) => {
          getSP().web.getFileByServerRelativePath(url).setContent(buffer);
        });
      });
    });

This block of code works perfectly fine for reading data but as soon as I try to set the content of a changed worksheet, it changes the MIME-TYPE to "application/zip" and the workbook changes state to a "read-only" mode.

Another part of this error is that the reason Excel gives for being in a read-only mode and "repairing" itself is this AutoFilter table change.
image

I want to continue to use tables in the excel files that are being edited and removing the tables and reverting them back to their regular ranges removes the issue.

How can I ensure that the mime-type stays the same and that the workbook does not "break" and enter the read-only state?

@patrick-rodgers
Copy link
Member

what does excel's writeBuffer() produce?

@hyperliskdev
Copy link
Author

writeBuffer provides a Promise<Buffer> that contains the edits made to the workbook.
image

They seem to use their own type for Buffer that extends ArrayBuffer, there is a stack overflow question talking about how this excelJS.Buffer breaks the excel file. Athough there isnt a set solution that I have tested from this aspect.
image

@hyperliskdev
Copy link
Author

writeBuffer provides a Promise<Buffer> that contains the edits made to the workbook. image

They seem to use their own type for Buffer that extends ArrayBuffer, there is a stack overflow question talking about how this excelJS.Buffer breaks the excel file. Athough there isnt a set solution that I have tested from this aspect. image

It doesnt look like this solution works in either casting to any or casting to plain JS Buffer.

@hyperliskdev
Copy link
Author

image
writeBuffer(); seems to output a Uint8Array

@hyperliskdev
Copy link
Author

hyperliskdev commented May 22, 2024

image

_testingExcel = async (): Promise<void> => {
    const url =
      "______/something.xlsx";

    let workbook = new Excel.Workbook();

    await this._sp.web
      .getFileByServerRelativePath(url)
      .getBuffer()
      .then((buffer) => {
        console.log(buffer);                     // <-- 1
        workbook.xlsx.load(buffer);
        console.log(workbook);              // <-- 2
      });

    let excelBuffer = await workbook.xlsx.writeBuffer();
    console.log(excelBuffer);                 // <-- 3

    let setContent =  await this._sp.web.getFileByServerRelativePath(url).setContent(excelBuffer);
    console.log(setContent);                 // <-- 4
  };

@hyperliskdev
Copy link
Author

hyperliskdev commented May 22, 2024

image
This section of code is from the ExcelJS load function that I am using. This section could be the culprint for why the MIME-TYPE changes to "application/zip".

image
It could also be related to this section here because the Content-Type header is not set and sharepoint may default to application/zip.

@hyperliskdev
Copy link
Author

This seems to be an issue with ExcelJS, I will close this now as this has appeared here.

Copy link

github-actions bot commented Jun 1, 2024

This issue is locked for inactivity or age. If you have a related issue please open a new issue and reference this one. Closed issues are not tracked.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants