Get sheet name while importing a spreadsheet

Posted by: priyatama.bhadke on 31 March 2025, 3:04 am EST

  • Posted 31 March 2025, 3:04 am EST

    Hi ,

    I am implementing a spreadsheet import process, and during the import, I want to validate the name of “Sheet1.” If “Sheet1” is named “test,” the system should proceed with the import and display an alert saying ready to upload. However, if “Sheet1” is named “hello,” the import should be canceled, and the existing data on the spreadsheet should remain unchanged.

    Regards,

    Priya B

  • Posted 31 March 2025, 6:42 am EST - Updated 31 March 2025, 6:47 am EST

    Hi Priya,

    As per my understanding, you want to validate the sheet name while importing a file—for example, allowing the import if Sheet1 is named “test” and canceling it if the name is “hello.”

    You can achieve this by using a dummy Spread instance. First, import the file into the dummy Spread, retrieve the sheet name, and validate it. If the name matches “test,” proceed with importing it into the original Spread instance and display the required message. If the name is “hello,” prevent the file from being imported into the original Spread instance.

    Refer to the code snippet, attached GIF, and sample: https://jscodemine.mescius.io/share/9DVBT88S60SJF4-C5Kxmcw/?defaultOpen={"OpenedFileName"%3A["%2Findex.html"%2C"%2Fsrc%2Fapp.jsx"]%2C"ActiveFile"%3A"%2Fsrc%2Fapp.jsx"}

    document.getElementById("import").addEventListener("click", (e) => {
        let file = document.getElementById("file").files[0];
    
        if (!file) {
            console.log("File not selected!");
            return;
        }
    
        let validateSpread = new GC.Spread.Sheets.Workbook();
    
        validateSpread.import(file, () => {
            console.log("File imported!!");
    
            let sheet1Name = validateSpread.getSheet(1).name();
    
            if (sheet1Name) {
                if (sheet1Name === "test") {
                    alert("Ready to upload!");
    
                    spread.import(file, () => {
                        alert("Imported Successfully!!");
                    }, (err) => {
                        console.log(err);
                    });
                } else if (sheet1Name === "hello") {
                    alert("Importing cancelled!");
                }
            }
        }, (err) => {
            console.log(err);
        });
    });

    GIF:

    Regards,

    Priyam

  • Posted 2 April 2025, 3:51 am EST - Updated 2 April 2025, 3:56 am EST

    Hi Priyam,

    Thanks for your reply !!

    Please find attached screenshot , I am using designer and from there I am using import functionality, Your code doesn’t not work with it. Can you please help me with this case ?

    Regards,

    Priya B

  • Posted 3 April 2025, 4:57 am EST - Updated 3 April 2025, 5:03 am EST

    Hi Priya,

    To validate the sheet during import via the Designer UI, you need to override the import method. In this override, first load the sheet into a dummy spread for validation. If the validation passes, then proceed with loading it into the current spread.

    Refer to the snippet below, along with the attached GIF and sample: https://jscodemine.mescius.io/share/DFLWjkfopE27oSyu4rfjiA/?defaultOpen={"OpenedFileName"%3A["%2Findex.html"]%2C"ActiveFile"%3A"%2Findex.html"}

    let flag = false;  
    
    let oldfn = GC.Spread.Sheets.Workbook.prototype.import;  
    
    GC.Spread.Sheets.Workbook.prototype.import = function () {  
        let file = arguments[0];  
        let validateSpread = new GC.Spread.Sheets.Workbook();  
    
        if (flag) {  
            flag = false;  
            return oldfn.apply(this, arguments);  
        } else {  
            flag = true;  
        }  
    
        validateSpread.import(file, () => {  
            let sheetName = validateSpread.getSheet(1).name();  
    
            if (sheetName !== "hello") {  
                return oldfn.apply(this, arguments);  
            } else {  
                return oldfn.apply(new GC.Spread.Sheets.Workbook(), arguments);  
            }  
        }, (err) => {  
            console.log(err);  
        });  
    };  

    GIF:

    Regards,

    Priyam

Need extra support?

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

Learn More

Forum Channels