Skip to main content
natrev
Legend
February 23, 2016
Question

Read and write data to Excel using javascript..!

  • February 23, 2016
  • 2 replies
  • 8731 views

Hi Everyone!

I have one excel document named as Temp.xls which is contains filename and file path.

Now I need check that Excel file name with active document name, If match found we need to update the current path of active document to existing file path location.

Please suggest me if any other solution much appreciated.

yajiv

My code is :

UpdateExcel();

function UpdateExcel(){       

    var docRef=app.activeDocument;

    var docPath=docRef.path;

    var dname=docRef.name;

       

    var f = docPath+"/Temp.xls";

    var datafile = new File(f);

    if (datafile.exists) {

    datafile.open('a') ;

    while (datafile.eof){

        strLineIn = datafile.readln();

        colorArray = strLineIn.split(",");

        if (colorArray[1]!=""){

            if(colorArray[1].match(dname)!=null){

                colorArray[2]=docPath;

            }

        }

    }

}

}

This topic has been closed for replies.

2 replies

Chuck Uebele
Community Expert
Community Expert
February 24, 2016

Change the 'r' in line 10 to a 'w' so that you can write to the file. Or if that doesn't work. read the file to a variable, make all your changes to the data in that variable, then write the entire variable back to the file.

//read the file

if (textFile.exists) {

textFile.encoding = "UTF8";

textFile.lineFeed = "unix";

textFile.open("r", "TEXT", "????");

var str = textFile.read();

textFile.close();

//code here to alter the array with your correctons.

//write the file

textFile.encoding = "UTF8";

textFile.open("w", "TEXT", "????");

textFile.write("\uFEFF");

textFile.lineFeed = "unix";

textFile.write(str);

textFile.close();

}

Participant
May 21, 2024

You can also check this code

<script type="text/javascript">
    function UploadProcess() {
        //Reference the FileUpload element.
        var fileUpload = document.getElementById("fileUpload");
 
        //Validate whether File is valid Excel file.
        var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.xls|.xlsx)$/;
        if (regex.test(fileUpload.value.toLowerCase())) {
            if (typeof (FileReader) != "undefined") {
                var reader = new FileReader();
 
                //For Browsers other than IE.
                if (reader.readAsBinaryString) {
                    reader.onload = function (e) {
                        GetTableFromExcel(e.target.result);
                    };
                    reader.readAsBinaryString(fileUpload.files[0]);
                } else {
                    //For IE Browser.
                    reader.onload = function (e) {
                        var data = "";
                        var bytes = new Uint8Array(e.target.result);
                        for (var i = 0; i < bytes.byteLength; i++) {
                            data += String.fromCharCode(bytes[i]);
                        }
                        GetTableFromExcel(data);
                    };
                    reader.readAsArrayBuffer(fileUpload.files[0]);
                }
            } else {
                alert("This browser does not support HTML5.");
            }
        } else {
            alert("Please upload a valid Excel file.");
        }
    };
    function GetTableFromExcel(data) {
        //Read the Excel File data in binary
        var cfb = XLS.CFB.read(data, {type: 'binary'});
        var workbook = XLS.parse_xlscfb(cfb);
 
        //get the name of First Sheet.
        var Sheet = workbook.SheetNames[0];
 
        //Read all rows from First Sheet into an JSON array.
        var excelRows = XLS.utils.sheet_to_row_object_array(workbook.Sheets[Sheet]);
 
        //Create a HTML Table element.
        var myTable  = document.createElement("table");
        myTable.border = "1";
 
        //Add the header row.
        var row = myTable.insertRow(-1);
 
        //Add the header cells.
        var headerCell = document.createElement("TH");
        headerCell.innerHTML = "Id";
        row.appendChild(headerCell);
 
        headerCell = document.createElement("TH");
        headerCell.innerHTML = "Name";
        row.appendChild(headerCell);
 
        headerCell = document.createElement("TH");
        headerCell.innerHTML = "Country";
        row.appendChild(headerCell);
        
        headerCell = document.createElement("TH");
        headerCell.innerHTML = "Age";
        row.appendChild(headerCell);
        
        headerCell = document.createElement("TH");
        headerCell.innerHTML = "Date";
        row.appendChild(headerCell);
         
         headerCell = document.createElement("TH");
        headerCell.innerHTML = "Gender";
        row.appendChild(headerCell);
 
 
        //Add the data rows from Excel file.
        for (var i = 0; i < excelRows.length; i++) {
            //Add the data row.
            var row = myTable.insertRow(-1);
 
            //Add the data cells.
            var cell = row.insertCell(-1);
            cell.innerHTML = excelRows[i].Id;
 
            cell = row.insertCell(-1);
            cell.innerHTML = excelRows[i].Name;
 
            cell = row.insertCell(-1);
            cell.innerHTML = excelRows[i].Country;
            
            cell = row.insertCell(-1);
            cell.innerHTML = excelRows[i].Age;
            
            cell = row.insertCell(-1);
            cell.innerHTML = excelRows[i].Date;
            
            cell = row.insertCell(-1);
            cell.innerHTML = excelRows[i].Gender;
        }
        
 
        var ExcelTable = document.getElementById("ExcelTable");
        ExcelTable.innerHTML = "";
        ExcelTable.appendChild(myTable);
    };
</script>

Reference: https://qawithexperts.com/article/javascript/read-excel-file-using-javascript-xlsx-or-xls/2393

Tom Ruark
Inspiring
February 23, 2016

I would export (Save As) your xls file as CSV (Comma Separated Values/Comma Delemited). Temp.csv

You will need to make sure your data cells do not contain any commas and any special characters are handled correctly.

natrev
natrevAuthor
Legend
February 24, 2016

Hi Tom,

Thanks for your valuable reply.

Already I have use that CSV format, unfortunate I can read data but can't to update the data to CSV.

Please advice, which Mode is  favorable for simultaneously read and write data.

- yajiv

Code:

UpdateExcel(); 

function UpdateExcel(){         

    var docRef=app.activeDocument; 

    var docPath=docRef.path; 

    var dname=docRef.name; 

         

    var f = docPath+"/Temp.csv"; 

    var datafile = new File(f); 

    if (datafile.exists) {  

        datafile.open('r'); 

        while (!datafile.eof){ 

            strLineIn = datafile.readln(); 

            colorArray = strLineIn.split(","); 

            if (colorArray[6]!=""){ 

                if(colorArray[6].match(dname)!=null){ 

                    colorArray[7].value=docPath; 

                    return;

                } 

            } 

        } 

    }