开发者

Paste CSV values into several Form fields in any web page

The Problem:

There are many good Scientific Calculator pages on the Web.

Some Calculator pages have a large text area, where you can just paste your input CSV values, directly.

But...some Calculator Forms require you to enter/paste EACH input value into a separate Form input field!

 [x1] [x2] [x3] ...etc.

That's laborious if yo开发者_StackOverflow社区u want to enter many data points, many times...

Look at this ex. Calculator: http://zweigmedia.com/RealWorld/multlinreg.html

Another ex.: http://zweigmedia.com/RealWorld/newgraph/regressionframes.html

See? You need to enter/paste each input value individually...argh!

The Question:

Is there a Bookmarklet or AHK Autohotkey script, which would simply paste many input CSV values, (maybe copied from an Excel spreadsheet or other data input source), into all input Form fields of the Calculator, AT ONCE?.

This would be really useful if it's a GENERIC script/bkmlt, (ie: for ANY such Calculator Forms in the web)...

thks! SFdude Win XP SP3


I do not know of any such scripts, but you could quite easily write one yourself or hire someone to do it (vworker.com). The method would be to use the COM.ahk module found in the forums and send javascript to the browser (I know this works for IE at least).

One would have to customize the javascript for every calculator, though. I cannot imagine a generic way to do this.


I'm assuming you already know how to get the data from the CSV. Inputing the data into the webpage is as simple as parsing the input table & entering the data:

wb := ComObjCreate("InternetExplorer.Application")
wb.Navigate("http://zweigmedia.com/RealWorld/multlinreg.html")
wb.Visible := true
while wb.busy
    sleep 10

n := 0
table := wb.document.theForm.all.tags("table")[0]

; loop through all the rows
; skip Row 0 since its headers - A_Index starts at 1 anyways
Loop % table.rows.length - 1 {
    ; access the cells in the row
    cells := table.rows[A_Index].cells
    ; loop through the cells
    Loop % cells.length
        ; each cell has an input element - access & set the value of this element
        cells[A_Index-1].childNodes[0].value := n++
}

This example uses AutoHotkey_L


The task is not very hard.

I). Preparations

1). Make some good CSV data.

I suppose your example input looks like

1.1,2.2,3.3
10.1,20.2,30.3
100.1,200.2,300.3

I.e.: your input has

  • numbers
  • dots (for fractions)
  • commas (,) for delimiting the values in one line
  • new lines (\n) for delimiting the lines

2). Then we will split the data properly.

3). And then put each values to their proper places.

Both instruments (multlinreg.html and newgraph/regressionframes.html) have the same code to get the k-th input from the j-th row of the table (j starts from 1, k starts from 0):

document.getElementsByTagName('table')[1].getElementsByTagName('tr')[j].getElementsByTagName('input')[k].value;

II.1. Bookmarklet: new line break

First of all, do you know how to make a bookmarklet? You can just save any page as a bookmark in your browser, and then replace its URL for the code in a specific format.

Here is the code for the bookmarklet (works with both of your tools):

javascript:(function(){
    var insCSV = prompt('insert CSV');
    insCSV.trim();
    var arrInsCSV = [];

    /*Split the CSV and put it into a two-dimensional array*/
    for (i=0; i<insCSV.split('\r\n').length; i++){
        arrInsCSV.push(insCSV.split('\r\n')[i].split(','));
    }

    /*Give info on values. If CSV is bigger than the table, there might be an error in the CSV, so the script might fail*/
    alert(arrInsCSV.length +' rows in CSV ' +'and '+ arrInsCSV[0].length +' values in CSV');

    /*Loop through the table and insert the values. j+1 is due to the table layout*/
    var tabData = document.getElementsByTagName('table')[1];
    for (j=0; j<arrInsCSV.length; j++){
        for (k=0; k<arrInsCSV[j].length; k++){
            tabData.getElementsByTagName('tr')[j+1].getElementsByTagName('input')[k].value = arrInsCSV[j][k];
        }
    }
})();

So now the steps are the following:

1). Copy the CSV-data with proper formatting.

2). Open the instrument.

3.1). Execute the bookmarklet.

3.2). Paste the data to the prompt, and you are good.

II.2. Bookmarklet: semicolon break

Just to be on a safe side. The code above is supposed to work with its \n\r, if you copy the CSV and paste it to the prompt properly.

But if you fail for some reason, you could use semicolons (;) instead of new lines (\n) for delimiting lines. Then your example input could look like that:

1.1,2.2,3.3;10.1,20.2,30.3;100.1,200.2,300.3

In this case replace the corresponding part of the code with

    /*Split the CSV and put it into a two-dimensional array*/
    for (i=0; i<insCSV.split(';').length; i++){
        arrInsCSV.push(insCSV.split(';')[i].split(','));
    }

That is how you can insert the values of a quasi-CSV to the input table cells with a simple bookmarklet, without AHK!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜