Search code examples
node.jsexceljs-xlsxsheetjs

Node.js - get superscript and subscript in excel


I am parsing an excel which has superscripts and subscripts and want to enclose them like this :

 <sup>superscripted value</sup> 
 <sub>subscripted value</sub>

tried using xlsx, excel parser and SheetJS to try and identify if the value has been superscripted/subscripted.

Is there any other way I can identify if the value has been subscripted/superscripted?


Solution

  • try using xlsx-populate

    let sup_sub_map = {
        "superscript": "<sup>",
        "superscriptEnd": "</sup>",
        "subscript": "<sub>",
        "subscriptEnd": "</sub>",
    };
    
    //assuming you have parsed till cell value:
    cellValue.foreach(sub_value => {
        if (sub_value['children'].length > 1) {
           let type = sup_sub_map[sub_value['children'][0]["children"][0]["attributes"]["val"]];
           let endType = sup_sub_map[sub_value['children'][0]["children"][0]["attributes"]["val"] + "End"];
           newVal = newVal + type + sub_value['children'][1]["children"][0] + endType
        } else {
            newVal = newVal + sub_value["children"][0]["children"][0];
        }
    });
    

    Its not the cleanest or the best way to do this but certainly gets the job done. Havent come across any other library which gives a subscript/superscript identifier.