SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with Javascript and xls(code example included!)

    This is the code I'm working with:

    At about the bottom of the code, you'll see <div id="excel">
    In there I'm supposed to keep the CVS of my excel file. Does anyone know how to use ACTIVEX to either:
    -convert excel to cvs
    -convert .xml to cvs
    I'm fine with either method.
    This is just running on the P.C, so i'm fine with using ActiveX. If you don't know how, if you know how, please give me an alternative.

    Or, if you know how to edit this code, to allow it to read from an XML file, please tell me how. I've been struggling on it. I'm pretty bad with code, so please help me out. Thanks a lot!


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>CSV Search</title>

    <style><!--

    body
    {
    background-color: white;
    color: black;
    font-family: arial, helvetica, sans-serif;
    }

    /* The result-found highlight style for
    a matching string, can be changed.
    For example, you could give it a yellow
    background-color (see comment).
    */

    .hilite
    {
    color: red;
    /* background-color: yellow; */
    font-weight: bold;
    }

    /* Define column width;
    Leave out specific column
    for auto-sizing depending
    on cell-contents:
    */

    .column1
    {
    width: 100px;
    }

    .column2
    {
    width: 150px;
    }

    table
    {
    border-collapse: collapse;
    border: 1px solid black;
    }

    td
    {
    text-align: left;
    vertical-align: top;
    padding: 6px;
    border: 1px solid black;
    }

    /* Two row background-colors
    to seperate rows; can be changed:
    */

    tr td
    {
    background-color: #eee;
    }

    tr.alternateRow td
    {
    background-color: #ccc;
    }

    #excel
    {
    display: none;
    }

    --></style>

    <script type="text/javascript"><!--

    // Row and column separator
    // characters, can be changed:

    var rowSeparator = "~";
    var columnSeparator = ";";

    var resultsI = 0;

    function searchData()
    {
    var data = getData();
    var search = document.getElementById("searchString").value;
    var arrRows = data.split(rowSeparator);
    var rowsLength = arrRows.length;

    var s = "";

    search = replaceStr(search, " ", " ");
    var phraseSearch = ( search.indexOf("\"") >= 0 );

    arrHeader = arrRows[0].split(columnSeparator);
    headerLength = arrHeader.length;
    header = "";
    for (var i = 0; i < headerLength; i++)
    {
    header += "<th>" + arrHeader[i] + "</th>";
    }
    header = "<tr>" + header + "</tr>";

    if (phraseSearch)
    {
    resultsI = 0;
    search = replaceStr(search, "\"", "");
    s += getMatchingRows(arrRows, search);

    if (s != "")
    {
    s = "<p>" + resultsI +
    " results found for phrase \"" +
    search + "\"/p> " +
    "<table>" + header + s + "</table>";
    }

    }
    else
    {
    var searchWords = search.split(" ");
    var wordsLength = searchWords.length;
    for (var i = 0; i < wordsLength; i++)
    {
    resultsI = 0;
    if (searchWords[i] != "")
    {
    var thisS = getMatchingRows(arrRows,
    searchWords[i]);
    if (thisS != "")
    {
    thisS = "<p>" + resultsI +
    " results found for \"" +
    searchWords[i] + "\"/p> " +
    "<table>" + header + thisS + "</table>";
    s += thisS;
    }
    }
    }
    }

    if (resultsI < 1)
    {
    // No results output
    s = "<p><em>No results matching \"" +
    search + "\" were found.</em></p>";
    }

    var outputLayer = document.getElementById("dataOutput");
    outputLayer.innerHTML = s;

    document.getElementById("h1data").innerHTML = "Results";
    searchForm.style.display = "none";
    newSearchLink.style.display = "block";
    }

    function newSearch()
    {
    var outputLayer = document.getElementById("dataOutput");
    outputLayer.innerHTML = "";
    searchForm.style.display = "block";
    newSearchLink.style.display = "none";
    document.getElementById("h1data").innerHTML = "Search Data";
    }

    function getMatchingRows(arrRows, search)
    {
    var sRow = "";
    var isAlternate = false;
    var rowsLength = arrRows.length;
    var lowSearch = search.toLowerCase();
    for (var i = 1; i < rowsLength; i++)
    {
    var row = arrRows[i];
    if ( row.toLowerCase().indexOf(lowSearch) >= 0 )
    {
    // Construct a table row with matches
    // to display later:

    var classString = (isAlternate) ?
    " class=\"alternateRow\"" : "";
    sRow += "<tr" + classString + ">";

    arrColumns = row.split(columnSeparator);
    var columnLength = arrColumns.length;
    for (var j = 0; j < columnLength; j++)
    {
    var column = arrColumns[j];
    sRow += "<td class=\"column" + (j + 1) + "\">";
    var rowDisplay = replaceStr(column,
    search,
    "<span class=\"hilite\">" + search +
    "</span>");
    var rowDisplay = wrapString(column,
    search,
    "<span class=\"hilite\">",
    "</span>");

    sRow += rowDisplay;
    sRow += "</td>";
    }
    sRow += "</tr>";

    isAlternate = !isAlternate;
    resultsI++;
    }
    }
    return sRow;
    }

    function getData()
    {
    var excelLayer = document.getElementById("excel");
    return excelLayer.firstChild.data;
    }

    function wrapString(sAll, sFind, sStart, sEnd)
    {
    var sAllLow = sAll.toLowerCase();
    var sFindLow = sFind.toLowerCase();
    var strPos = sAllLow.indexOf(sFindLow);

    var isThere = (strPos >= 0);

    var sNew = "";
    if (isThere)
    {
    var sBefore = sAll.substring(0, strPos);
    var sMiddle = sAll.substring(strPos, strPos + sFind.length);
    var sAfter = sAll.substring(strPos + sFind.length);
    sNew = sBefore + sStart + sMiddle + sEnd + sAfter;
    }
    else
    {
    sNew = sAll;
    }
    return sNew;
    }

    function replaceStr(str, oldStr, newStr)
    {
    regExp = new RegExp(oldStr, "gi");
    results = str.replace(regExp, newStr);
    return results;
    }

    // --></script>
    </head>
    <body>

    <h1 id="h1data">Search Data</h1>

    <form id="searchForm">
    <p>
    Your query:
    <input type="text" size="20"
    name="searchString" id="searchString" />
    <input type="text" name="suppressReturn"
    style="display: none;" />
    <input type="button" onclick="searchData()"
    value="Search" />
    </p>
    </form>

    <div id="dataOutput">
    &nbsp;
    </div>

    <div id="newSearchLink" style="display: none;">
    <p>[<a href="javascript:newSearch()">New search ...</a>]</p>
    </div>

    <div id="excel">
    Header 1;Header 2;Header 3~d;3;40~this;is a;test~this;is a
    second;test~testing yet;again;...
    </div>

    <p>Version 2.1, Copyright &copy; 2003 by Ken</p>

    </body>
    </html>

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,992
    Mentioned
    186 Post(s)
    Tagged
    2 Thread(s)

    ActiveX controls

    Hi legolas52, welcome to the forums,
    Please do me a favor and edit your post putting your code inside &#91;HTML]&#91;/HTML] bb tags.
    The ActiveX for CSV files is
    HTML Code:
    <object id="sourceFileData" classid="clsid:333C7BC4-460F-11D0-BC04-0080C7055A83">
    <param name="DataURL" value="test.csv" />
    <param name="UseHeader" value="true" />
    </object>
    .....
    <div sourceFileData="Artist">
    <div sourceFileData="Song">
    <div sourceFileData="Format">
    ......
    test.csv
    Code:
    Artist,Song,Format
    Donovan,Atlantis,LP
    Cat Stevens,Peace Train,45
    Steppenwolf,Magic Carpet Ride,LP
    Don McLean,American Pie,8-Track
    Emerson Lake & Palmer,Tarkus,8-Track
    Janis Joplin,Me and Bobby McGee,LP
    Cat Stevens,Wild World,45
    and the ActiveX for XML files is (you might have a different version)
    HTML Code:
    .....
    <script type="text/javascript">
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
    xmlDoc.async="false"
    xmlDoc.load("note.xml")
    nodes = xmlDoc.documentElement.childNodes
    to.innerText = nodes.item(0).text
    from.innerText = nodes.item(1).text
    header.innerText = nodes.item(2).text
    body.innerText = nodes.item(3).text
    </script>
    .....
    <b>To: </b><span id="to"></span>
    <br />
    <b>From: </b><span id="from"></span>
    <br />
    <b><span id="header"></span></b>
    <br />
    <span id="body"></span>
    .....
    note.xml
    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    <note>
      <to>legolas52</to>
      <from>Mittineague</from>
      <heading>XML ActiveX example</heading>
      <body>This is example text.</body>
      <myfoot>And this is more example text.</myfoot>
    </note>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •