Hi there,
I'm doing a completely messy hack of an HTML table. I want to copy the contents of the table into the clipboard so users can easily paste the data into an email.
Currently I can get all the data out, but I need the headers to line up with the fields of data. Sometimes fields are long, sometimes short, so doing it by tab is a bit fiddly.
Is there an easy way to format data nicely (so it looks like a neat table) in javascript?
I want it to look like this:
Ref., Commodity, Commission, USD
19292 Tin 1/12 4000
93838 Aluminium Al 3/30 30300003
93923 Copper 1/12 1029
Currently, I can't get the headers to line up about the columns, and it looks like this (there a more columns which makes it look even worse, this is just for illustrative purposeS):
Ref.,Commodity,Commission,USD
19292 Tin 1/12 4000
93838 Aluminium Al 3/30 30300003
93923 Copper 1/12 1029
This is my code at present (this shows all the columns I want to use):
function HTMLToClipboard()
{
//copy the contents of the html page into the clipboard so the
//data can be pasted into Excel or an email
var output = new String();
FillCount = document.all.tdRef.length
var Ref = new String();
var TimeFilled = new String();
var PlacedBy = new String();
var Commodity = new String();
var Account = new String();
var Quantity = new String();
var Currency = new String();
var Direction = new String();
var Commission = new String();
var Price = new String();
var USDPrice = new String();
var Prompt = new String();
var nRef = new Array();
var nFillData = new Array();
//populate the array
nRef = document.all('tdRef')
nTimeFilled = document.all('tdTimeFilled')
nPlacedBy = document.all('tdPlacedBy')
nCommodity = document.all('tdCommodity')
nAccount = document.all('tdAccount')
nQuantity = document.all('tdQuantity')
nCurrency = document.all('tdCurrency')
nDirection = document.all('tdDirection')
nCommission = document.all('tdCommission')
nPrice = document.all('tdPrice')
nUSDPrice = document.all('tdUSDPrice')
nPrompt = document.all('tdPromptDate')
for (x=0;x<FillCount;x++)
{
Ref = nRef[x].innerText
TimeFilled = nTimeFilled[x].innerText
PlacedBy = nPlacedBy[x].innerText
Commodity = nCommodity[x].innerText
Account = nAccount[x].innerText
Quantity = nQuantity[x].innerText
Currency = nCurrency[x].innerText
Direction = nDirection[x].innerText
Commission = nCommission[x].innerText
Price = nPrice[x].innerText
USDPrice = nUSDPrice[x].innerText
Prompt = nPrompt[x].innerText
//take a new line
output = output + Ref + ",\t" + TimeFilled + ",\t" + PlacedBy + ",\t" + Commodity + ",\t" + Account + ",\t" + Quantity + ",\t" + Currency + ",\t" + Direction + ",\t" + Commission + ",\t" + Price + ",\t" + USDPrice + ",\t" + Prompt + "\n"
}
// put in the headers
output = "Ref" + ", FillTime" + ", Placed By" + ", Commodity" + ", Account" + ", Quantity" + ", Currency" + ", Direction" + ", Commission" + ", Price" + ", USD Price" + ", Prompt Date" + "\n\n" + output
//copy to clipboard
clipboardData.clearData();
clipboardData.setData('Text', output);
alert("Data has been copied to clipboard.\n")
}
If anyone can suggest how best to format text in javascript so it can be neatly pasted I would be VV grateful.
thanks!
graeme
I'm doing a completely messy hack of an HTML table. I want to copy the contents of the table into the clipboard so users can easily paste the data into an email.
Currently I can get all the data out, but I need the headers to line up with the fields of data. Sometimes fields are long, sometimes short, so doing it by tab is a bit fiddly.
Is there an easy way to format data nicely (so it looks like a neat table) in javascript?
I want it to look like this:
Ref., Commodity, Commission, USD
19292 Tin 1/12 4000
93838 Aluminium Al 3/30 30300003
93923 Copper 1/12 1029
Currently, I can't get the headers to line up about the columns, and it looks like this (there a more columns which makes it look even worse, this is just for illustrative purposeS):
Ref.,Commodity,Commission,USD
19292 Tin 1/12 4000
93838 Aluminium Al 3/30 30300003
93923 Copper 1/12 1029
This is my code at present (this shows all the columns I want to use):
function HTMLToClipboard()
{
//copy the contents of the html page into the clipboard so the
//data can be pasted into Excel or an email
var output = new String();
FillCount = document.all.tdRef.length
var Ref = new String();
var TimeFilled = new String();
var PlacedBy = new String();
var Commodity = new String();
var Account = new String();
var Quantity = new String();
var Currency = new String();
var Direction = new String();
var Commission = new String();
var Price = new String();
var USDPrice = new String();
var Prompt = new String();
var nRef = new Array();
var nFillData = new Array();
//populate the array
nRef = document.all('tdRef')
nTimeFilled = document.all('tdTimeFilled')
nPlacedBy = document.all('tdPlacedBy')
nCommodity = document.all('tdCommodity')
nAccount = document.all('tdAccount')
nQuantity = document.all('tdQuantity')
nCurrency = document.all('tdCurrency')
nDirection = document.all('tdDirection')
nCommission = document.all('tdCommission')
nPrice = document.all('tdPrice')
nUSDPrice = document.all('tdUSDPrice')
nPrompt = document.all('tdPromptDate')
for (x=0;x<FillCount;x++)
{
Ref = nRef[x].innerText
TimeFilled = nTimeFilled[x].innerText
PlacedBy = nPlacedBy[x].innerText
Commodity = nCommodity[x].innerText
Account = nAccount[x].innerText
Quantity = nQuantity[x].innerText
Currency = nCurrency[x].innerText
Direction = nDirection[x].innerText
Commission = nCommission[x].innerText
Price = nPrice[x].innerText
USDPrice = nUSDPrice[x].innerText
Prompt = nPrompt[x].innerText
//take a new line
output = output + Ref + ",\t" + TimeFilled + ",\t" + PlacedBy + ",\t" + Commodity + ",\t" + Account + ",\t" + Quantity + ",\t" + Currency + ",\t" + Direction + ",\t" + Commission + ",\t" + Price + ",\t" + USDPrice + ",\t" + Prompt + "\n"
}
// put in the headers
output = "Ref" + ", FillTime" + ", Placed By" + ", Commodity" + ", Account" + ", Quantity" + ", Currency" + ", Direction" + ", Commission" + ", Price" + ", USD Price" + ", Prompt Date" + "\n\n" + output
//copy to clipboard
clipboardData.clearData();
clipboardData.setData('Text', output);
alert("Data has been copied to clipboard.\n")
}
If anyone can suggest how best to format text in javascript so it can be neatly pasted I would be VV grateful.
thanks!
graeme