How to open a SJS file in NodeJs

Posted by: nguyenvu.work on 24 January 2024, 9:36 pm EST

    • Post Options:
    • Link

    Posted 24 January 2024, 9:36 pm EST

    • Hi! I am using
      spread.open
      to open a SJS file in nodejs, my input data is ‘Buffer’ or “Blob”. But spread.open only accept a File while File is not defined in nodejs. How can I open a SJS file which is Blob or Buffer in Nodejs?
  • Posted 28 January 2024, 6:32 pm EST

    Hi,

    Kindly refer to the following code snippet and the attached sample for importing the “.sjs” file in the node environment:

    	var wb = new GC.Spread.Sheets.Workbook();
    	const data = fs.readFileSync('./export.sjs');
    	const blob = new Blob([data.buffer], { type: "application/zip" });
    	blob.name = "export.sjs";
    	blob.stream = fs.createReadStream('./export.sjs');
    	wb.open(blob, function () {
    		let sheet = wb.getActiveSheet();
    		console.log('-------load sjs file-------');
    		console.log(sheet.name());
    		console.log(sheet.getValue(3, 4));
    	}, function (e) {
    		console.log(e); // error callback
    	}, {
    		includeUnusedStyles: false
    	});
    
    	var wb1 = new GC.Spread.Sheets.Workbook();
    	const data1 = fs.readFileSync('./blank.xlsx');
    	const blob1 = new Blob([data1.buffer], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
    	blob1.name = "blank.xlsx";
    	blob1.stream = fs.createReadStream('blank.xlsx');
    	wb1.import(blob1, function () {
    		console.log('-------load excel file---------');
    		let sheet = wb1.getActiveSheet();
    		console.log(sheet.name());
    		console.log(sheet.getArray(0, 0, 3, 1));
    	}, function (msg) {
    		console.log(msg); // error callback
    	}, {
    		fileType: GC.Spread.Sheets.FileType.excel,
    	});

    In the attached sample, kindly set the Valid License Key for the application to work correctly (line no. 20 on index.js) file. The sample uses the latest version of SpreadJS( v17.0.2) and therefore, set the valid License Key for V17. If you are using the V16, set the valid License Key for V16.

    If you are still face the issue, kindly do share the sample with us. You could also modify the attached sample and share it with us with the issue you are facing.

    Regards,

    Ankit

    sjs_node.zip

  • Posted 28 January 2024, 9:49 pm EST

    Hi.,

    • Thanks for your example, I tested it and it’s work. But in my context, the input data come from a request, so I cannot create the “stream” by “fs.createReadStream(‘./export.sjs’);”
    • I tried to create other one as: “blob.stream = Readable.from([data]);” then I got an error from “wb.open” as “{ errorCode: 1, errorMessage: ‘Incorrect file format.’ }”. This is the code after editing:
      var wb = new GC.Spread.Sheets.Workbook();
      const data = fs.readFileSync('src/myFolder/export.sjs');
      const blob = new Blob([data.buffer], { type: 'application/zip' });
      blob.name = 'export.sjs';
      blob.stream = Readable.from([data]);
      wb.open(
        blob,
        function () {
          let sheet = wb.getActiveSheet();
          console.log('-------load sjs file-------');
          console.log(sheet.name());
          console.log(sheet.getValue(3, 4));
        },
        function (e: any) {
          console.log(e); // Got: { errorCode: 1, errorMessage: 'Incorrect file format.' }
        },
        {
          includeUnusedStyles: false,
        }
      );
    
  • Posted 28 January 2024, 10:11 pm EST - Updated 28 January 2024, 10:16 pm EST

    Hi,

    I tested with the mentioned code snippet i.e.,

    blob.stream = Readable.from([data]);

    and it is working as expected. The error might happen if you have not set the License Key for V17 or an invalid License Key is set.

    Could you kindly set a valid License Key and try again? If you are still face the issue, kindly do share the sample along (with the License Key that you are using) with us by creating the ticket on our private portal at: https://developer.mescius.com/my-account/my-support

    Kindly don’t share the License Key on public forum. You may refer to the attached gif that shows the steps I have performed.

    Regards,

    Ankit

  • Posted 29 January 2024, 6:43 pm EST

    Hi,

    Thank you so much, after setting valid License, it worked. I thought v16’s License and v17’s License are the same but not. Thanks again!!

  • Posted 30 January 2024, 8:24 pm EST - Updated 30 January 2024, 8:30 pm EST

    Hi,

    I got an other issue in NodeJs environments, in my case I need to open a xlsx file from a request then save file into data base under SJS format. But the exported SJS file has lose some graphic data like image, shape, chart (Maybe some other type that I don’t know yet). This is the code:

    const fs = require('fs');
    const mockBrowser = require('mock-browser').mocks.MockBrowser;
    const fileReader = require('filereader');
    const canvas = require("canvas");
    const { Blob } = require("buffer");
    
    global.window = mockBrowser.createWindow();
    global.document = window.document;
    global.navigator = window.navigator;
    global.self = global;
    global.HTMLCollection = window.HTMLCollection;
    global.getComputedStyle = window.getComputedStyle;
    global.FileReader = fileReader;
    global.canvas = canvas;
    
    const GC = require('@grapecity/spread-sheets');
    const { Readable } = require('stream');
    require('@grapecity/spread-sheets-io');
    
    // Set the Valid License Key as per the SpreadJS Version
    GC.Spread.Sheets.LicenseKey = "My valid key";
    
    try {
    	var wb1 = new GC.Spread.Sheets.Workbook();
    	const data1 = fs.readFileSync('./default.xlsx'); // File's content contain images, shape, Chart,...
    	const blob1 = new Blob([data1.buffer], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
    	// const blob1 = new Blob([data1.buffer], { type: "application/zip" });
    	blob1.name = "default.xlsk";
    	blob1.stream = Readable.from([data1]);
    
    	wb1.import(blob1, function () {
    		wb1.save(async (data) => {
    			console.log('-------saved sjs---------');
    			const arrayBuffer = await data.arrayBuffer();
    			const buffer = Buffer.from(arrayBuffer);
    			fs.writeFileSync("./savedsjs.sjs", buffer)  // This file is lose data
    		})
    
    		fs.writeFileSync("./imported.xlsx", data1) // The data of this file is intact
    	}, function (msg) {
    		console.log(msg); // error callback
    	}, {
    		fileType: GC.Spread.Sheets.FileType.excel,
    	});
    } catch (e) {
    	console.error("** Error manipulating spreadsheet **");
    	console.error(e);
    }
    
    • I tried this in browser environments and it worked!
    • SpreadJs version: 16.2.0
  • Posted 31 January 2024, 10:58 pm EST

    Hi,

    It seems like you haven’t imported the dependencies for the images, charts and therefore, they are not imported/exported.

    Kindly include the correct dependencies for shapes, charts, etc, and it should work fine.

    For charts and shapes, include the following:

    import "@grapecity/spread-sheets-shapes";
    import '@grapecity/spread-sheets-charts';

    Kindly try with the required dependencies and if you still face the issue, kindly do share the excel file with us

    .

    Regards,

    Ankit

  • Posted 1 February 2024, 1:23 pm EST

    Hi,

    Thank you! My issue is resolved. So, I must import all of the sub lib to ensure the file’s data is intact?

    Is there any statement like one for all?

  • Posted 1 February 2024, 3:54 pm EST

    Hi,

    We are happy that your issue is resolved.

    Yes, you need to import all the modules like shapes, charts in your application based on your usage.

    Currently, there is no one module for all as it would increase the bundle size. You just import the modules, you need. For example, if you don’t need the tablesheet, just don’t import the “tablesheet” module.

    Regards,

    Ankit

  • Posted 1 February 2024, 6:17 pm EST

    I got it!

    Thank you again for your supports.

  • Posted 5 February 2024, 5:28 pm EST

    Hi,

    Still relate to open file in NodeJS environment by “spread-sheets-io”, I got an other issue when I use “spread.open” or “spread.import”, I got an error: “{ errorCode: 1, errorMessage: ‘Incorrect file format.’ }”. But this only happen with some file (file in attachment), despite I imported all the sub-libbrary

    require("@grapecity/spread-excelio");
    require("@grapecity/spread-sheets");
    require("@grapecity/spread-sheets-barcode");
    require("@grapecity/spread-sheets-charts");
    require("@grapecity/spread-sheets-io");
    require("@grapecity/spread-sheets-languagepackages");
    require("@grapecity/spread-sheets-pivot-addon");
    require("@grapecity/spread-sheets-print");
    require("@grapecity/spread-sheets-resources-ja");
    require("@grapecity/spread-sheets-shapes");
    require("@grapecity/spread-sheets-tablesheet");
    
    • I tried on Web enviroments and there are not any problems
  • Posted 5 February 2024, 5:30 pm EST

  • Posted 6 February 2024, 8:15 pm EST

    Hi,

    I was able to replicate the issue with the SpreadJS v16.2.0. However, the issue is fixed in the latest version of SpreadJS. I tried with the SpreadJS V16.2.6 and it was working fine with that version.

    Also, I tested with the latest version V17.0.3 and it was also working with the latest version. I recommend that you upgrade to the latest version (or to the V16.2.6). It is always advised to use the latest version as it includes bug fixes, performance enhancements and new features.

    Also, kindly create separate cases for new queries/issues. The original query of this case has already been answered. In the new case, you could add the reference for previous cases also. It helps us to manage the cases effectively and to reply quickly.

    Regards,

    Ankit

Need extra support?

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

Learn More

Forum Channels