Saving excel file in Spread Js format throws 'Incorrect file format.'

Posted by: mpoluri on 21 August 2024, 1:07 pm EST

  • Posted 21 August 2024, 1:07 pm EST - Updated 21 August 2024, 1:10 pm EST

    Hi,

    I am trying to convert an excel buffer data to spread js buffer in NodeJs environment hosted on AWS lambda. I have the Spread Js license correctly set and I have all the global variables set along with canvas. I’m encountering ‘Incorrect file format’ when trying to import the blob to workbook. Here is the code snippet:

    async function convertXlsxBufferToSjs(buffer: ArrayBuffer) {
      // create a new workbook
      const workbook = new GC.Spread.Sheets.Workbook();
    
      // import array buffer data to a Blob
      const blob = new Blob([buffer], {
        type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
      });
      console.log("entering workbook import");
    
      return new Promise<Blob>((resolve, reject) => {
        workbook.import(
            //@ts-expect-error Blob should also work when importing using SpreadJs
            blob,
            () => {
              workbook.save(
                  async (blob: Blob) => {
                    resolve(blob);
                  },
                  (e: unknown) => {
                    console.log("Error saving spreadsheet");
                    console.error(e);
                    reject(e);
                  },
                  {
                    includeEmptyRegionCells: false,
                    includeCalcModelCache: true,
                  }
              );
            },
            (e: unknown) => {
              console.log("Error loading blob, entered failure callback", e);
              reject(e);
            },
            {
              fileType: GC.Spread.Sheets.FileType.excel,
              doNotRecalculateAfterLoad: true,
              calcOnDemand: true,
            }
        );
      });
    }

    Here the workbook.import() calls the failure callback with “Error loading blob, entered failure callback { errorCode: 1, errorMessage: ‘Incorrect file format.’ }”

    can you please help me resolve this issue, thanks.

    Maneesha

  • Posted 22 August 2024, 5:19 am EST - Updated 22 August 2024, 5:25 am EST

    Hi Maneesha,

    I tested with the latest version of SpreadJS V17.1.4 and it seems to be working fine at my end. You could refer to the attached sample and the gif that shows the steps.

    Steps to Run the Sample:

    1. Install the dependencies using the “npm install” command.

    2. Run the application using the “node index.js” command (apply license before running this command).

    After running the application, you could see that it shows the blob size, its type and the code of the success callback function of the save() method is executed. In the sample, the Blob is created from the buffer similar to yours.

    Later, you could save the blob or convert it to the file or buffer depending on your use case.

    Kindly try with the sample that I have shared. If you still encounter the issue, kindly do share the sample with us or you could also modify the attached sample and share back.

    Note:

    1. Make sure to apply the correct license key of SpreadJS v17 at line no. 24 of “index.js” file.

    2. SpreadJS, yet officially don’t support the Node environment.

    Regards,

    Ankit

    sjs_node_update.zip

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels