Ok, I'm new to javascript (learning quickly though). I've been able to cobble together some code which does a couple of things.
Opens/reads an excel file (local now but will be on server). Get
cell A1 (will always be a #) - variable repCount.
Writes cells A2-A33 to a table in html (hard coded start and stop)
Now what I would it to do is
take the repCount variable as the ending row in the writing part - dynamic stop point
write it to an array that I can manipulate (should be globally available).
Here's my code so far:
<script language="javascript" >
// Open the spreadsheet and get the count of reps
function GetData(cell,row)
{
var excel = new ActiveXObject("Excel.Application");
var excel_file = excel.Workbooks.Open("SOMEFILE.xlsx");
var excel_sheet = excel.Worksheets("Sheet1");
var repCount = excel_sheet.Cells(cell,row).Value;
document.getElementById('div1').innerText = repCount;
}
// open spreadsheet and populate table with representatives
var excelApp=null, excelFile=null, excelSheet=null;
var filename = "SOMEFILE.xlsx";
function initExcel(filename)
{
excelApp = new ActiveXObject("Excel.Application");
excelFile = excelApp.Workbooks.Open(filename);
excelSheet = excelApp.Worksheets('Sheet1');
}
function myShutdownExcel()
{
excelApp.Quit();
excelApp=null;
excelFile=null;
excelSheet=null;
}
function myGetData(column, row)
{
return excelSheet.Cells(column, row).Value;
}
function byId(e) {return document.getElementById(e);}
function myOnLoad2()
{
var numRows = 33, numCols = 1;
var tBody = byId('dataTableBody');
var rowIndex, colIndex, curVal;
var curRow, curCell, curCellText;
initExcel(filename);
for (rowIndex=2; rowIndex<=numRows; rowIndex++)
{
curRow = document.createElement('tr');
for (colIndex=1; colIndex<=numCols; colIndex++)
{
curVal = myGetData(rowIndex, colIndex);
curCell = document.createElement('td');
curCell.setAttribute('title', 'The value of cell [' + rowIndex + ',' + colIndex +']\nis: ' + curVal);
curCellText = document.createTextNode(curVal);
curCell.appendChild(curCellText);
curRow.appendChild(curCell);
}
tBody.appendChild(curRow);
}
myShutdownExcel();
}
</script>
</head>
<body>
<p> </p>
<div style="background: #009955; width:'100%';" align="center">
<font color="#000080" size="12pt">
<b>Get data from excel sheets</b>
</font>
</div>
<center>
<p> </p>
<div id="div1" style="background: #DFDFFF; width:'100%';" align="center">
To start the statistics page - Click the button below
</div>
<input type="button" value="Start" onClick="GetData(1,1);" />
<input type="button" value="Next" onClick="myOnLoad2()" />
</center>
<b>Get data from excel sheets</b>
<table id='dataTable'>
<tbody id='dataTableBody'>
</tbody>
</table>
</body>
</html>
Am I on the right track? I can only use javascript so don't try pointing me to jQuery or PHP. Can anyone help or point me to a good resource, I do want to learn this but syntax and me...not such good friends. Once I see an example I'm pretty good about figuring out how to adapt it but getting it into an array has boggled me for the last week. Thanks in advance.
Related
I want the user to input both 'part ID' and 'quantity' through prompt and have those values added to a table; which I've managed so far. After that, I want to add another row below the first one using the same method resulting in 2 rows with different values etc.
<html>
<head>
</head>
<!--CREATE AND POPULATE TABLE -->
<body onload="partID(); qty()">
<table id="resultsTable" border=".5px" class="results">
<tr><th>Part ID</th><th>Quantity</th>
<tr>
<td id="partID">Part ID</td>
<td id="qty">Quantity</td>
</tr>
</table>
<br>
<!-- I want this f('createTable') to bring the prompt back and append to existing table onclick, if that makes sense -->
<button onclick="createTable()">Add Another Part</button>
</body>
<!-- LOCAL SCRIPTS -->
<script>
function partID(){
var partID = prompt("Enter part ID:");
var x = document.getElementById('partID');
x.innerHTML = partID;
}
function qty(){
var qty = prompt("Enter Quantity:");
var y = document.getElementById('qty');
y.innerHTML = qty;
}
</script>
</html>
I can get it to work once around but I'm not sure how to repeat it for a new row and without losing previous data.
What you want to do is append data to the table, right now you are setting the values of individual cells instead of just appending them to the already existing ones.
JavaScript has a neat little shortcut for appending (just like many other languages) which is +=, basically var myVar = 'Foo'; myVar += 'Bar'; is equal to var myVar = 'Foo'; myVar = myVar + 'Bar';
function add() {
//prompt the user with boxes for the ID and quantity
var partID = prompt("Enter part ID:");
var qty = prompt("Enter Quantity:");
//generate the HTML for a new table row and insert the given values
var table_html = "<tr><td>" + partID + "</td><td>" + qty + "</td></tr>";
//append the HTML to the already existing HTML in the table
document.getElementById('resultsTable').innerHTML += table_html;
}
/*I dont like default buttons*/
button {
background-color: lightgrey;
color: black;
padding: 8px;
border: 0px;
}
button:hover {
background-color: grey;
}
<html>
<head>
</head>
<body onload="add();">
<!-- instead of onload use a button so the user can repeat the action multiple times -->
<button onclick="add();">Add part</button>
<hr>
<table id="resultsTable" border=".5px" class="results">
<tr>
<th>Part ID</th>
<th>Quantity</th>
</tr>
</table>
<br>
</body>
</html>
I hope this helps, if you need further explanation about the code just leave a comment.
Good luck.
From what I understand, you want to be able to add a new row to the <table>. For this, you probably want to use a button.
<button onclick="addRow()">Add row</button>
Then you can add a row using insertAdjacentHTML :
function addRow() {
var table = document.getElementById('resultsTable');
var partID = prompt("Enter part ID:");
var qty = prompt("Enter Quantity:");
table.insertAdjacentHTML('beforeend', "<tr><td>" + partID + "</td><td>" + qty + "</td></tr>")
}
Using insertAdjacentHTML is safer and more efficient than replacing the whole table innerHTML.
If I click on the submit button, then insert new cell, and click more than one time cells inserts again and again in a table i want to only add items only one time in table not again and again.
i want to call clear previous data and add new one when call fillTable().
my code is below here
<html>
<body>
<input type="button" onclick="fillTable()" value="submit">
<div id="out" >
<h2>Results are</h2>
<table id="tblData" border="1">
<tr>
<th>Name</th>
<th>Share</th>
</tr>
</table>
</div>
<script>
function fillTable(){
clearCells(); // clear previous
var tableRef = document.getElementById("tblData");
var rowcount = tableRef.rows.length;
var newRow = tableRef.insertRow(rowcount);
// Insert a cell in the row at index 0
var newCell = newRow.insertCell(0);
var newCell2 = newRow.insertCell(1);
var newText1 = document.createTextNode("Hello");
var newText2 = document.createTextNode("12000");
newCell.appendChild(newText1);
newCell2.appendChild(newText2);
newCell.appendChild(newText1);
newCell2.appendChild(newText2);
}
function clearCells(){
var tableRef = document.getElementById("tblData");
tableRef.deleteCell(0);
tableRef.deleteCell(1);
}
</script>
</body>
</html>
your problem was in your clearCell method, I fixed it for you:
function clearCells(){
var tableRef = document.getElementById("tblData");
if(tableRef.rows.length > 1){
tableRef.deleteRow(1);
}
}
I'm a newbie and figure out the possibilities of google spreadsheet scripts. I try to read out the column titles of my spreadsheat and to load it into the html side of the google script. I'm able to read out a whole array but I can't figure out how to read out each single range (e.q. B1, C1, D1,...) of a table. The next problem i have is, that I don't know beforehand the number of columns that I have to bring into the html.
Who can help me?
------ code.gs -------
function doGet() {
return HtmlService.createHtmlOutputFromFile('ProbeGUI')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
//return HtmlService.createHtmlOutput('<b>Hello, world!</b>');
}
function WriteToSpreadsheet(input) {
var ss = SpreadsheetApp.openById("1PkakB0cZKtzSxJUFULZjJBEPb-7U6XOASI69yOopWRM"); //Der Sheetname lautet ProbeCode
var sheet = ss.getSheetByName("Datenpool");
var firstEmptyRow = sheet.getLastRow() + 1;
var range = sheet.getRange("B"+(firstEmptyRow)); //In " " steht, in welche Spalte geschrieben wird
range.setValue(input);
}
function WriteToFormular() {
var ss = SpreadsheetApp.openById("1PkakB0cZKtzSxJUFULZjJBEPb-7U6XOASI69yOopWRM");
var sheet = ss.getSheetByName("Datenpool");
var LastColumn = sheet.getLastColumn();
// var range = sheet.getDataRange();
var range = sheet.getRange(1, 2, 1, LastColumn);
var values = range.getValues();
// return values[0][ColumnCount];
// Browser.msgBox(values);
return values;
}
---- html code ----
<script language="JavaScript" type="text/javascript">
function SendData() {
google.script.run.WriteToSpreadsheet(document.Formular.IdeenWert.value);
}
function ReadSpreadsheet() {
alert("ReadSpreadsheet");
// google.script.run.withSuccessHandler(add_Item_row).WriteToFormular();
google.script.run.withSuccessHandler(ChooseItem).WriteToFormular();
}
function add_Item_row(ItemNamen) {
var parent = document.getElementById('newDiv');
var child = document.createElement('p');
// var NewItem = ItemNamen[0][1];
child.innerHTML = ItemNamen;
parent.appendChild(child);
}
function ChooseItem(SpreadsheetSpaltenNamen){
alert(SpreadsheetSpaltenNamen);
var i = 0;
alert(i);
while (SpreadsheetSpaltenNamen[0][i] != "") {
function add_Item_row(SpreadsheetSpaltenNamen[0][i])
i++;
}
}
function validate_num(formdata) {
formdata.value=formdata.value.replace(/\D/, '' );
}
</script>
<body onload="ReadSpreadsheet();">
<form name="Formular" action="" >
<div style="position:relative; width:100px; top:20px; " >Restbetrag:</div><div style="position:relative; left:100px; width:100px; height:30px;" id="RestGeld">100</div>
<div style="float:left; width:100px;" id="Item">Item</div><div><input id="WerteID" name="IdeenWert" type="text" size="7" onkeyup="validate_num(this)" onblur="formSubmit(this.value)" /></div>
<div style="clear:both"></div>
<div style="position:relative; top:20px; left:100px; " ><input onclick="SendData();" type="button" value="OK" /></div> <!-- google.script.host.close() -->
</form>
<div id='vendordiv'>
<p>
<input type="Button" value="Spreadsheet auslesen" onclick="ReadSpreadsheet()"/>
</p>
</div>
<div id ='newDiv'>
</div>
</body>
I tried your code and got some errors in the client side. so i made a change just to test the button "Spreadsheet auslesen"
onclick="google.script.run.withSuccessHandler(ChooseItem).WriteToFormular();"
I was not able to find the reason of why making the call on the way you are doing it does not work, i will update the answer when i find it.
also in the function "ChooseItem" inside the while loop, you have "function", delete it so you can call the function instead of trying to create a new one.
after those modification i was able to insert the titles in the html.
hope it helps.
I have one client side application which is rendering maps on browser(IE). In that application I have one HTML form which will capture some text contents and then the application will be generating one Word document, which will have all the fields submitted by the user in a tabular form. I am able to do that, but i have to insert one image from the browser(the screenshot of the map).
I divided the problem into two parts:
1) write some table contents in tabular form using javascript--- I am able to do that.
2) take screenshot of map and insert it into that word document.--- couldn't take screenshot, but tried with one dummy image from local machine and insert it into word doc.
The problem is, I want that image in fourth row and first column. But the "word.Selection"
points to first cell in table.
itable.columns(1).cells(5).Range.Text="some text"; cant we use something like this to insert text or place "word.Selection" in a particular cell.
the below page serves my form request.
<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.11.0.js"></script>
<script type="text/javascript">
function myFunction()
{
var table = document.getElementById("myTable");
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = getFieldValue("FirstName");
cell2.innerHTML = getFieldValue("lastName");
}
var JPEGName = "C:\\all data\\MyBT\\tasks\\dp manifold.jpg";
function saveAsWord() {
word = new ActiveXObject("Word.Application");
word.visible=false;
word.Documents.Add();
word.Application.Visible = true;
word.ActiveDocument.PageSetup.LineNumbering.Active=false;
word.ActiveDocument.PageSetup.TopMargin=30;
word.ActiveDocument.PageSetup.BottomMargin = 30;
word.ActiveDocument.PageSetup.LeftMargin = 35;
word.ActiveDocument.PageSetup.RightMargin = 30;
word.ActiveDocument.PageSetup.Gutter = 0.0;
word.Selection.Font.Bold = true;
word.Selection.Font.Size = 14;
word.Selection.ParagraphFormat.Alignment = 1;
word.Selection.TypeText("form details");
word.Selection.Font.Size = 12;
var itable=word.ActiveDocument.Tables.Add(word.ActiveDocument.Application.Selection.Range, 16, 4);
//itable.AutoFormat(16);
word.ActiveDocument.Tables(1).Range.ParagraphFormat.Alignment = 0;
itable.columns(2).cells(1).Range.ParagraphFormat.Alignment =1;
itable.columns(2).cells(1).Range.Text="Exchange Area"
itable.columns(3).cells(1).Range.ParagraphFormat.Alignment =0;
itable.columns(1).cells(2).Range.Text="CSS Job No ";
itable.columns(3).cells(2).Range.Text="Sr. No. : ";
itable.columns(1).cells(3).Range.Text="Customer Details : "+getFieldValue("firstName");
itable.columns(1).cells(4).Range.Text="Engineers Details : "+getFieldValue("lastName");
itable.columns(3).cells(4).Range.ParagraphFormat.Alignment =0;
itable.columns(3).cells(4).Range.Text="Date : ";
//itable.columns(3).cells(4).Range.Content.InlineShapes.AddPicture(JPEGName);
itable.columns(1).cells(5).Range.Text="Requirement : ";
itable.columns(3).cells(5).Range.ParagraphFormat.Alignment =0;
itable.columns(3).cells(5).Range.Text="Survey Reqd : ";
itable.columns(1).cells(6).Range.Text="Visiting Date : ";
itable.columns(2).cells(6).Range.ParagraphFormat.Alignment =1;
itable.columns(2).cells(6).Range.Text="Visit Time : ";
itable.columns(1).cells(5).Range.Text="Whom To Meet : ";
itable.columns(3).cells(5).Range.ParagraphFormat.Alignment =0;
itable.columns(3).cells(5).Range.Text="Underground Cable : ";
itable.columns(1).cells(6).Range.Text="Visiting Date : ";
itable.columns(2).cells(6).Range.ParagraphFormat.Alignment =1;
itable.columns(2).cells(6).Range.Text="Overhead Work : ";
itable.columns(3).cells(6).Range.Text="Underground Civil : ";
itable.columns(1).cells(7).Range.Text="Location: ";
itable.columns(1).cells(8).Range.Text="Map Ref: ";
itable.columns(1).cells(10).Range.ParagraphFormat.Alignment =1;
itable.columns(1).cells(10).Range.Text="Title: EXPOSE BURIED JOINT";
itable.columns(3).cells(10).Range.ParagraphFormat.Alignment =1;
itable.columns(3).cells(10).Range.Text="Job Summary";
itable.columns(1).cells(12).Range.ParagraphFormat.Alignment =1;
itable.columns(1).cells(12).Range.Text="Sig. of Security Supervisior";
itable.columns(3).cells(12).Range.ParagraphFormat.Alignment =1;
itable.columns(3).cells(12).Range.Text="Sig. of Meeting Person";
itable.Cell(3, 1).Merge(itable.Cell(3, 2));
itable.Cell(4, 2).Split(1, 2);
itable.Cell(4, 1).Merge(itable.Cell(4, 2));
itable.Cell(4, 2).Merge(itable.Cell(4, 3));
itable.Cell(5, 2).Split(1, 2);
itable.Cell(5, 1).Merge(itable.Cell(5, 2));
itable.Cell(5, 2).Merge(itable.Cell(5, 3));
itable.Cell(7, 1).Merge(itable.Cell(7, 3));
itable.Cell(8, 1).Merge(itable.Cell(8, 3));
itable.Cell(9, 1).Merge(itable.Cell(9, 3));
itable.Cell(11, 1).Merge(itable.Cell(11, 3));
itable.Cell(13, 1).Merge(itable.Cell(13, 3));
itable.Cell(14, 1).Merge(itable.Cell(14, 3));
itable.Cell(15, 1).Merge(itable.Cell(15, 3));
itable.Cell(16, 1).Merge(itable.Cell(16, 3));
//word.Application.PrintOut(true);
//setTimeout("appexit()",10000);
word.Selection.TypeParagraph();
word.Selection.InlineShapes.AddPicture(JPEGName);
}
</script>
</head>
<body>
<table id="myTable" style="border:1px solid black">
<tr>
<td>First Name</td>
<td>Last Name</td><td>SSN</td>
</tr>
</tr>
</table><br><br><br>
Your Social security number is <script type="text/javascript">
document.write(getFieldValue("ssn"))
</script>.<br>
You entered "<script type="text/javascript">
document.write(getFieldValue("FirstName"))
</script>" as your First Name.<br>
You entered "<script type="text/javascript">
document.write(getFieldValue("lastName"))
</script>" as your Last Name.<br>
<button id="foo" onclick="myFunction()">Try it</button>
<input type="button" value="print slip" onclick="saveAsWord()"/>
</body>
</html>
I am new to writing script for generating MS document.
I have found out the code where I was doing wrong. Instead of using "word.Selection" it would be good to use "itable.cell(row,column).range" for inserting.
itable.cell(5,1).range.InlineShapes.AddPicture(JPEGName );
I apologize if this kind of question has already been asked but I have looked through "Related Questions" and I don't think anything in here matches with mine, so please bear with me as I try to present my problem and how am I going to go about this, so here I go.
Okay, I will refer you my screenshot. Two of my tables have been generated by using Javascript and one is static and I didn't comment out the tbody tags within a listModulesWithinScene table.
http://img852.imageshack.us/i/haildstabletable.jpg/ (click in the link to view the image)
Here's my cut-down version of my HTML code for those who are interested. I'm pretty much new to this website, even though I do lurk through here from search engine (Google).
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link href='http://fonts.googleapis.com/css?family=Josefin+Sans:100,regular' rel='stylesheet'
type='text/css' />
<link href="cssHomeAutomaionInterface.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="Scripts/SwitchContent.js"></script>
<script type="text/javascript" src="Scripts/ScrollingImagesInHeader.js"></script>
<script type="text/javascript" src="Scripts/XMLReaderWriter.js"></script>
<script type="text/javascript" src="Scripts/Main.js"></script>
<script type="text/javascript" src="Scripts/PageSpecific/Home.js"></script>
</head>
<body>
<!-- ... -->
<div id="home" class="container">
<div id="menuHome" class="submenu">
</div>
<div id="contentHome" class="content">
<h1>Home Page</h1>
<div id="moduleListArea">
<table id="listModules" class="data">
<caption>Lights and Devices</caption>
<thead>
<tr>
<th style="width: 100px;">Module ID#</th>
<th>Module Name</th>
<th style="width: 60px;">Status</th>
<th style="width: 150px">Action</th>
</tr>
</thead>
<!--<tbody>
<tr style="color: Green;" title="Dimmer">
<th>AB.CD.EF</th>
<td>2x Floor Lamp</td>
<td>75%</td>
<td>
Toggle
<div style="float: right;">
<input style="width: 40px;" id="module1" value="75" />
Set
</div>
</td>
</tr>
<tr style="color: Blue;" title="Switch">
<th>AB.CD.EE</th>
<td>Bedside Fan</td>
<td>ON</td>
<td>
Toggle
</td>
</tr>
</tbody>-->
</table>
</div>
<div id="sceneListArea" style="width: 50%; float: left;">
<table id="listScenes" class="data">
<caption style="text-align: left;">Scenes</caption>
<thead>
<tr>
<th>Scene Name</th>
<th style="width: 80px">Action</th>
</tr>
</thead>
<!--<tbody>
<tr>
<td>Welcome Home</td>
<td>Toggle</td>
</tr>
<tr>
<td>All Lights Off</td>
<td>Toggle</td>
</tr>
</tbody>-->
</table>
</div>
<div id="modulesWithinSceneListArea"
style="width: 50%; float: right;">
<table id="listModulesWithinScene" class="data">
<caption style="text-align: right;">Lights and Devices Within Scene</caption>
<thead>
<tr>
<th style="width: 100px;">Module ID#</th>
<th>Scene Name</th>
<th style="width: 50px">Level</th>
</tr>
</thead>
<tbody>
<tr>
<th>1</th>
<td>2x Floor Lamp</td>
<td>50%</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<!-- ... -->
</body>
</html>
</code>
Now, let me go ahead and describe my problem.
So anyway, I want to call a function that's in the part of the code:
// Create a new anchor tag and name it "List."
var a_set = document.createElement("a");
a_set.href = "javascript:ListAllModulesWithinScene(listScenes_row" + r +
"_toggle";<br />
a_set.id = "listScenes_row" + r + "_set";
a_set.appendChild(document.createTextNode("List"));
I want javascript:ListAllModulesWithinScene(...) that is inside quotes to call the function (ListAllModulesWithinScene(sceneName)) in PageSpecific/Home.js, but nothing happens if I click the List link; so far, it's not working. This is the function that I'm trying to call.
function ListAllModulesWithinScene(sceneName)
{alert("test");
/* The part of the code that I took out fills up the table with the list of modules
that are part of the scene, each module having different levels/settings. */
}
The expected result is that I want to see the alert message box just to make sure it works before I call out the code to generate the rows of data like say...
function ListAllModulesWithinScene(sceneName)
{
/*listModulesWithinScene = document.getElementById("listModulesWithinScene");
// Delete the tbody tag if it does not exist and create a enw tbody tag.
if(listModulesWithinScene.getElementsByTagName("tbody") != null)
listModulesWithinScene.removeChild("tbody");
var listModulesWithinScene_tBody = document.createElement("tbody");
var xmlRows = xmlObj.childNodes[2].getElementsByTagName("Scene");
for (var r = 0; r < xmlRows.length; r++)
{
if (xmlRows[r].getAttribute("name") == sceneName)
{
var moduleRow = xmlRows[r].getElementsByTagName("Module");
if (moduleRow.length > 0)
{
var row = document.createElement("tr");
for (var msr = 0; msr < moduleRow.length; msr++)
{
var moduleRow2 = xmlObj.childNodes[1].getElementsByTagName("Module");
for (var mr = 0; mr < xmlRow2.length; mr++)
{
if (moduleRow[mr].getAttribute("id") ==
xmlObj.childNodes[1].childNodes[mr].getAttribute("id"))
{
var td_id = document.createElement("th");
td_id.appendChild(
document.createTextNode(moduleRow.getAttribute("id")));
td_id.setAttribute("id", "listModulesFromScene_row" + r +
"_id");
row.appendChild(td_id);
var td_name = document.createElement("td");
td_name.appendChild(
document.createTextNode(moduleRow2.getAttribute("name")));
td_name.setAttribute("id", "listModulesFromScene_row" + r +
"_name");
row.appendChild(td_name);
var td_level = document.createElement("td");
td_level.appendChild(
document.createTextNode(moduleRow.getAttribute("level")));
td_level.setAttribute("id", "listModulesFromScene_row" + r +
"_level");
row.appendChild(td_level);
}
}
}
}
}
}
listModulesWithinScene_tBody.appendChild(row);
listModulesWithinScene.appendChild(listModulesWithinScene_tBody);*/
}
In order to help get a better ideal of how my tables are generated, I want to post my entire code. I could provide more details about my problem but that's all I can provide for now.
This is the XMLReaderWriter.js file that I currently have.
/*
When it comes to loading and saving an XML file, it helps to try to keep the XML file organized
and well-formed, so it's best to not to modify the XML file UNLESS you know what you're doing.
The structure of an XML file is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<HomeAutomationInterface>
<Setup>
<Location longitude="" latitude="" />
</Setup>
<Modules>
<!-- The first row is an example. -->
<Module id="1" name="module name" level="ON" type="dimmer">
<!-- ... -->
</Modules>
<Scenes>
<!-- the first row is an example. It can contain multiple modules. -->
<scene name="Scene Name">
<module id="1" level="50">
<module id="2" level="60">
<!-- ... -->
</scene>
<!-- ... -->
</Scenes>
<Timers>
<!-- The following four rows are an example. For DST, sunrise and sunset
is dependent in longitude and latitude. How this works is this:
1. Go to this website and enter your address information.
http://stevemorse.org/jcal/latlon.php
2. Go here and enter your today's date, followed by longitude and latitude,
and time zone: http://www.weatherimages.org/latlonsun.html
3. You should get your information related to sunrise and sunset. It should
look like this:
Thursday
10 March 2011 Universal Time - 5h
SUN
Begin civil twilight 06:30
Sunrise 06:54
Sun transit 12:48
Sunset 18:42
End civil twilight 19:06
Now that's how you get your times for your sunruse and sunset.
-->
<timer name="Timer Name 1" type="regular">
<regular time="hour:minute:second:millisecond">
<module id="1" level="50">
<!-- ... -->
</regular>
</timer>
<timer name="Timer Name 2" type="regular">
<regular time="hour:minute:second:millisecond">
<scene name="Scene Name">
<!-- ... -->
</regular>
</timer>
<timer name="Timer Name 3" type="DST">
<DST occour="sunrise|sunset" offsetDir="later|sooner"
offset="hour:minute:second:millisecond">
<module id="1" level="75">
<!-- ... -->
</DST>
</timer>
<timer name="Timer Name 4" type="DST">
<DST occour="sunrise|sunset" offsetDir="later|sooner"
offset="hour:minute:second:millisecond">
<scene name="Scene Name">
<!-- ... -->
</DST>
</timer>
<!-- ... -->
</Timers>
</HomeAutomationInterface>
It's a long documentation, but it helps to understand what this XML structure is all about.
*/
// Not for Internet Explorer 6 or below.
var xmlDoc;
var xmlObj;
// For HTML tables (<table>)
var listModules;
var listScenes;
var listModulesWithinScene;
// For HTML text boxes
var inputLongitude;
var inputLatitude;
function LoadXML()
{
// ActiveXObject will have to be checked first to see if it's defined. Otherwise, if you
// try to check XMLHttpRequest first, even if Internet Explorer supports it, you will get
// "Access Denied" in Internet Explorer and perhaps not in Firefox.
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load("HomeAutomationInterface.xml");
PopulateFromXML();
}
else
{
// If this is the only code in this function, then you will need to put all your
// project in the server, since Internet Explorer has a "Same Origin Policy" which
// I believe that the open method with "GET" causes a problem in Internet Explorer
// but did not cause a problem in Firefox. In order to rectify the problem, the code
// inside if(window.ActiveXObject) makes use of ActiveX.
xmlDoc = new XMLHttpRequest();
xmlDoc.open("GET", "HomeAutomationInterface.xml", null)
xmlDoc.send(null);
if (xmlDoc.status == 200)
{
xmlDoc = xmlDoc.responseXML;
PopulateFromXML();
}
}
}
function PopulateFromXML()
{
listModules = document.getElementById(
"listModules").getElementsByTagName("tbody");
// Gather the text fields for location data.
inputLongitude = document.getElementById("inputLongitude")
inputLatitude = document.getElementById("inputLatitude")
// Firefox's DOM Parser treats whitespaces as text nodes, including
// line breaks.
removeWhitespace(xmlDoc.documentElement);
// Send the document's root element to the XML Object variable.
xmlObj = xmlDoc.documentElement;
// Perform the checks and populate the tables
// and any other elements that are needed.
if (xmlObj.tagName == "HomeAutomationInterface")
{
// Check to be sure the first node is the "Setup" node. It contains the
// location node.
if ((xmlObj.childNodes[0].tagName == "Setup") &&
(xmlObj.childNodes[0].childNodes[0].tagName == "Location"))
{
// Copy the data from one of the attributes to the respective text boxes.
inputLongitude.value = xmlObj.childNodes[0]
.childNodes[0].getAttribute("longitude");
inputLatitude.value = xmlObj.childNodes[0]
.childNodes[0].getAttribute("latitude");
}
// The second node within the root element is Modules node.
// This will be implemented.
if (xmlObj.childNodes[1].tagName == "Modules")
{
//TODO: Implement the XML-to-Table translation that gets info
// about modules.
listModules = document.getElementById("listModules");
var listModules_tBody = document.createElement("tbody");
var xmlRows = xmlObj.childNodes[1].getElementsByTagName("Module");
for (var r = 0; r < xmlRows.length; r++)
{
var xmlRow = xmlRows[r];
var row = document.createElement("tr");
var td_id = document.createElement("th");
td_id.appendChild(document.createTextNode(xmlRow.getAttribute("id")));
td_id.setAttribute("id", "listModules_row" + r + "_id");
row.appendChild(td_id);
var td_name = document.createElement("td");
td_name.appendChild(document.createTextNode(xmlRow.getAttribute("name")));
td_name.setAttribute("id", "listModules_row" + r + "_name");
row.appendChild(td_name);
var td_level = document.createElement("td");
td_level.appendChild(document.createTextNode(xmlRow.getAttribute("level")));
td_level.setAttribute("id", "listModules_row" + r + "_level");
row.appendChild(td_level);
if (xmlRow.getAttribute("type") == "dimmer")
{
row.style.color = "Green";
// Create a new table cell for a dimmer. This will include a toggle,
// a text box, and a set button.
var td_dimmer = document.createElement("td");
// Create a new anchor tag and, set the href to #, and name it "Toggle."
var a_toggle = document.createElement("a");
a_toggle.href = "#";
a_toggle.id = "listMoudles_row" + r + "_dimmer_toggle";
a_toggle.appendChild(document.createTextNode("Toggle"));
td_dimmer.appendChild(a_toggle);
// Create a new div element to hold a text box and a set button.
var div_floatright = document.createElement("div");
div_floatright.style.float = "right";
// Create a new text box and append it to the div element.
var input_level = document.createElement("input");
input_level.type = "text";
input_level.name = "listModules_row" + r + "_inputLevel";
input_level.id = "listModules_row" + r + "_inputLevel";
input_level.style.width = "40px";
div_floatright.appendChild(input_level);
// Create a new anchor tag, set the href to #, and name it "Set."
var a_set = document.createElement("a");
a_set.href = "#";
a_set.id = "listMoudles_row" + r + "_dimmer_set";
a_set.appendChild(document.createTextNode("Set"));
div_floatright.appendChild(a_set);
// Append the div element to the table cell.
td_dimmer.appendChild(div_floatright);
// Finally, append the table cell to the row.
row.appendChild(td_dimmer);
}
else if (xmlRow.getAttribute("type") == "switch")
{
row.style.color = "Blue";
// Create a new table cell for a dimmer. This will include a toggle,
// a text box, and a set button.
var td_switch = document.createElement("td");
// Create a new anchor tag and, set the href to #, and name it "Toggle."
var a_toggle = document.createElement("a");
a_toggle.href = "#";
a_toggle.id = "listMoudles_row" + r + "_dimmer_toggle";
a_toggle.appendChild(document.createTextNode("Toggle"));
td_switch.appendChild(a_toggle);
row.appendChild(td_switch);
}
else
{
row.style.color = "Black";
row.appendChild(document.createTextNode("No actions available."));
}
listModules_tBody.appendChild(row);
}
listModules.appendChild(listModules_tBody);
// Uncomment this code and run the example.
//alert(listModules_row0_name.textContent);
}
// The third node within the root element is Scenes node.
// You need modules in order for scenes to work.
// This will be implemented.
if (xmlObj.childNodes[2].tagName == "Scenes")
{
listScenes = document.getElementById("listScenes");
var listScenes_tBody = document.createElement("tbody");
var xmlRows = xmlObj.childNodes[2].getElementsByTagName("Scene");
for (var r = 0; r < xmlRows.length; r++)
{
var xmlRow = xmlRows[r];
var row = document.createElement("tr");
var td_name = document.createElement("td");
td_name.appendChild(document.createTextNode(xmlRow.getAttribute("name")));
td_name.setAttribute("id", "listScenes_row" + r + "_name");
row.appendChild(td_name);
row.appendChild(td_name);
var td_actions = document.createElement("td");
// Create a new anchor tag and name it "Toggle."
var a_toggle = document.createElement("a");
a_toggle.href = "#";
a_toggle.id = "listScenes_row" + r + "_toggle";
a_toggle.appendChild(document.createTextNode("Toggle"));
// Create a new anchor tag and name it "List."
var a_set = document.createElement("a");
a_set.href = "javascript:ListAllModulesWithinScene(listScenes_row" + r +
"_toggle";
a_set.id = "listScenes_row" + r + "_set";
a_set.appendChild(document.createTextNode("List"));
td_actions.appendChild(a_toggle);
td_actions.appendChild(a_set);
row.appendChild(td_actions);
listScenes_tBody.appendChild(row);
}
listScenes.appendChild(listScenes_tBody);
}
// The last element is the Timers node.
// This will either activate the scene or turn on, off,
// dim, brighten, or set the light level of the module if
// it is the light module or turn on or off the appliance
// module. This will be implemented.
if (xmlObj.childNodes[3].tagName == "Timers")
{
//TODO: Implement a XML-to-Table parser that parses the XML data
// from the timers tag into the timer table.
}
}
}
// I stumbled across http://www.agavegroup.com/?p=32 and I borrowed the code from
// http://stackoverflow.com/questions/2792951/firefox-domparser-problem
// It took me a week to debug all my code until I find out what's going on with Firefox's
// DOM Inspector.
function removeWhitespace(node)
{
for (var i = node.childNodes.length; i-- > 0; )
{
var child = node.childNodes[i];
if (child.nodeType === 3 && child.data.match(/^\s*$/))
node.removeChild(child);
if (child.nodeType === 1)
removeWhitespace(child);
}
}
And now as for my background information, I'm doing this as my project for COP2822 (Scripting for the Web), so it's only Javascript and not server-side, so I have endured a lot of pain, but was able to make it through, so my experience with Javascript is well worth it in the end. I did have some experience with Javascript before I take COP2822, though.
Welcome to SO. Considering you copy pasted your code
a_set.href = "javascript:ListAllModulesWithinScene(listScenes_row"
+ r + "_toggle)";//<br />
You had a weird br tag in the middle of your js code and you had a missing ) in the function call.
If you still have issues, post and I'll try to discuss. I suggest getting a browser or extension to your browser that supplies a javascript console. In my opinion it's invaluable to debugging.
i think you want quotes (escaped) around the sceneName parameter value:
a_set.href = "javascript:ListAllModulesWithinScene(\"listScenes_row" + r + "_toggle\")";