Please tell me this is possible. I have a .txt file with data arranged like a spreadsheet (i would use excel instead but i dont have it and am broke) and would like to pull from it and assign as a variable.
Ex. (bla.txt)
first line is headers
Second is data
A|B|C
1|2|3
Would like to have them assigned as
Var x= A1
Var y= B2
Var z= C3
Any help would be appreciated.
Ps. All is on a closed circuit so no security issues.
It would be helpful if you could post your .txt file.
Also if you would like to use excel but cant afford it you should try LibreOffice or OpenOffice. They are both free and (at least LibreOffice is) FOSS.
To load this file into your javascript you can use Jquery to load it like this:
$.get('/path/to/data.txt', function(data) { //Gets file 'data.txt' and stores it in variable 'data'
//Code to execute after loading file
/*I’m not sure exactly how your .txt file is formatted so I don't know
How you want it stored into separate variables.
But you can do that here*/
});
Hopefully this at least helps you get started.
This simple code might help for extracting the data from individual lines.
Note: But this will work when you have one character only between |
example
A|B|C 1|2|3
but will not work for
AA|BC|CS 11|2|3
Here it is
var line = "A|B|C 1|2|3";
var x = line.charAt(0) + line.charAt(6);
var y = line.charAt(2) + line.charAt(8);
var z = line.charAt(4) + line.charAt(10);
Use other string functions to get your job done for the second case i pointed out.
Refer to this w3schools page for more help http://www.w3schools.com/js/js_string_methods.asp
Related
Something so trivial and I just can't seem to find an answer.
This is my code so far. However this is with the JSON alredy set as a variable.
I need it so that it gets the JSON text from
http://localhost:3001/sync/
and makes it equal to var txt. INSTEAD of the var txt = I want it so get the JSON text from the URL INSTEAD of the text I've added.
var txt = '{"loading":false,"playing":true,"position":0,"duration":389492,"index":13,"repeat":false,"shuffle":false,"volume":0.337499052286148,"context":{"uri":"spotify:user:#:playlist:66HXOPaG8wwe7k8t4YZj5b"},"contexts":[{"index":13,"descriptor":{"type":"list","uri":"spotify:user:#:playlist:66HXOPaG8wwe7k8t4YZj5b"}}],"track":{"artists":[{"image":"spotify:image:15a09a886f2149909821763f2f074cf1b7975574","images":[[64,"spotify:image:1aa2b5417668fdfc6966c9745b437e587d7ff23f"],[300,"spotify:image:15a09a886f2149909821763f2f074cf1b7975574"],[600,"spotify:image:865b8c83601ce2aef204a9c071fd2f531c12c000"],[1000,"spotify:image:5311029c2ba3de0b4e5d117b4e90d57b60720902"]],"name":"Duke Dumont","uri":"spotify:artist:61lyPtntblHJvA7FMMhi7E"}],"disc":1,"duration":389000,"image":"spotify:image:6f592ef177e159c00dd4f08049c4c962466b0776","images":[[64,"spotify:image:68fd12e77d374e7b9618ca0cf6786b9479837175"],[300,"spotify:image:6f592ef177e159c00dd4f08049c4c962466b0776"],[600,"spotify:image:6a30d6808f92167b4cb10eed2cf5f9838442d591"]],"name":"The Giver - Original Mix","number":2,"playable":true,"popularity":64,"starred":false,"explicit":false,"availability":"premium","album":{"uri":"spotify:album:66Io82H9e3b2rrtHFs2sE0"},"local":false,"advertisement":false,"placeholder":false,"uri":"spotify:track:6GbLDdBuFxZLDHhluGrrmA"}}';
var obj = eval ("(" + txt + ")");
document.getElementById("demo").innerHTML =
obj.playing;
It looks like you need to do use xmlhttprequest (ie. ajax). It is used to retrieve data from urls asynchronously.
You can either follow a tutorial to learn how to use it. Or you could learn how to use jquery (specifically jquery.get).
I'd suggest that if you don't know either, you either way start learning about xmlhttprequest but end up using jquery anyway.
And as the other post mentioned, once you learn how to retrieve data, use JSON.parse to parse the text to json, even though jquery can handle that automatically, too.
As far as I understand, u want the data to come from the URL and get assigned to variable txt.
In tat case you need to make a ajax call like this using jquery.
$.ajax({
url:"http://localhost:3001/sync/",
success:function(result){
var txt = result;
}});
U can now use JSON.parse(txt) to parse the json.
There is some data that I need to get from a local crawled page. It's inline javascript like this:
<script type="text/javascript">
//<![CDATA[
var graph_data = {"query":{"cid":["13908"],"timestamp_from":1402531200,"timestamp_till":1402531200,"views":1138942,"data":
etc, the variable is very long but you get the idea. I want to put "graph_data" into an array called $data. What is the best way to do this? I should add this is all being done by me locally & I don't need to execute any javascript code, just extract the data.
I have make a suggestion purely as an idea with some code worth trying!
As suggested, the DOM Document may provide this much easier, but here's another possible solution for extracting...
I'm guessing that the var graph_data is a JSON string that you want to extract from the HTML page so that you can store as a PHP var. The problem is that your code doesn't show how the variable ends but I'm going to assume that a new line break would be the way to identify the end of the variable being set. It may be a semi-colon though and if it is, instead of "\r\n" try ";"
// Assuming your html code is stored in $html...
preg_match("/var graph_data[^\{]*?(\{[^\r\n]+?)/is",$html,$match);
print "<pre>";
print_r($match);
print "</pre>";
This should result in $match[1] storing the part you need.
You can try passing that into json_decode() but that's touching some wood with crossed fingers.
Good luck...
Basically I have a string variable that is composed of two other variables separated by a comma:
item = "Pencil";
amount = 5;
entry = item + "," + amount;
*export entry to csv
So the "entry" variable should be in the correct format to save as a comma separated file. Is there some command that will take this variable and save it as a csv, or any other format easily opened in a spreadsheet for later use? The entry variable will change and the new information will need to be appended to the csv file if it already exists. So let's say if we also had:
item = "Paper";
amount = 25;
entry = item + "," + amount;
*export entry to csv
The resulting csv file should be:
Pencil,5
Paper,25
I've done a bit of searching through other questions, but most folks seem to be trying to do more complex things (e.g., dealing with server vs. client-side issues) while I'm just trying to figure out how to get data I'm working on my own computer in javascript to a saveable file. Seems like that isn't the case for many questions being asked though. I'm sure there's a simple answer out there and hopefully just a single command or two. However, I've been wading through a lot of semi-related posts that aren't too clear, so I figured this would be quicker.
It is perfectly possible, using some FileSaver implementation. I believe it exists natively on IE10, but still needs a shim on other browsers.
I've written a light-weight client-side CSV generator library that might come in handy. Check it out on http://atornblad.se/github/ (scroll down to the headline saying Client-side CSV file generator)
As I said, it requires a functioning FileSaver implementation handling calls to window.saveAs(). Check out Eli Grey's solution on http://eligrey.com/blog/post/saving-generated-files-on-the-client-side
When in place, you simply generate and save CSV files on the fly like this:
var propertyOrder = ["name", "age", "height"];
var csv = new Csv(propertyOrder);
csv.add({ name : "Anders",
age : 38,
height : "178cm" });
csv.add({ name : "John Doe",
age : 50,
height : "184cm" });
csv.saveAs("people.csv");
I am trying to add some JS to my pdf creation process to save some time. My goal is to basically click a button generate a PDF and have it print.
Right now I have:
var pp = this.getPrintParams();
pp.interactive = pp.constants.interactionLevel.full;
this.print(pp);
The only problem is that I don't know how to set the number of copies to print. I want pass a variable and print that many copies. The problem is that I can't really find any documentation discusses the methods in this class.
I know, this question is old. But I was looking for a solution, too. And I found here, that it should work with the following:
//no of copies
var n = 3;
var pp = this.getPrintParams();
//here is the magic
pp.NumCopies=eval(n);
this.print(pp);
For me, this is working with tcpdf and Adobe Reader.
Try this
var n = 3;
var pp = this.getPrintParams();
pp.NumCopies=eval(n);
this.print({bUI: false,bSilent: true,bShrinkToFit: true,printParams:pp});
Not possible, which is a good thing; since otherwise some websites would specify a high number and people who don't expect this would accidentally print lots of pages instead of just a single one.
You should call print method twice or more times, like the following:
this.print({bUI: false,bSilent: true,bShrinkToFit: true});
this.print({bUI: false,bSilent: true,bShrinkToFit: true});
I think it's not possible to set the number of copies.
See also:
Adobe's Print Production documentation
I'm looking to use a VBScript variable within a reference to a DOM element for a web-app I'm building. Here's a brief excerpt of the affected area of code:
dim num
num = CInt(document.myform.i.value)
dim x
x = 0
dim orders(num)
For x = 0 To num
orders(x) = document.getElementById("order" & x).value
objFile.writeLine(orders(x))
Next
This is my first venture into VBScript, and I've not been able to find any methods of performing this type of action online. As you can see in the above code, I'm trying to create an array (orders). This array can have any number of values, but that number will be specified in document.myform.i.value. So the For loop cycles through all text inputs with an ID of order+x (ie, order0, order1, order2, order3, order4, etc. up to num)
It seems to be a problem with my orders(x) line, I don't think it recognizes what I mean by getElementById("order" & x), and I'm not sure exactly how to do such a thing. Anyone have any suggestions? It would be much appreciated!
I was able to get this working. Thanks to both of you for your time and input. Here is what solved it for me:
Rather than using
document.getElementById("order" & x).value
I set the entire ID as a variable:
temp = "order" & x
document.getElementById(temp).value
It seems to be working as expected. Again, many thanks for the time and effort on this!
I can only assume that this is client side VBScript as document.getElementById() isn't accessible from the server.
try objFile.writeLine("order" & x), then check the source to make sure all the elements are in the document.
[As I can't put code in comments...]
That is strange. It looks to me like everything should be working.
Only other thing I can think of is: change
orders(x) = document.getElementById("order" & x).value
objFile.writeLine(orders(x))
to
orders(x) = document.getElementById("order" & x)
objFile.writeLine(orders(x).value)
It looks as if you're mixing client vs server-side code.
objFile.writeLine(orders(x))
That is VBScript to write to a file, which you can only do on the server.
document.getElementById
This is client-size code that is usually executed in JavaScript. You can use VBScript on IE on the client, but rarely does anyone do this.
On the server you'd usually refer to form fields that were part of a form tag, not DOM elements, (assuming you're using classic ASP) using request("formFieldName").
To make server-side stuff appear on the client (when you build a page) you'd embed it in your HTML like this:
<% = myVariable %>
or like this (as part of a code block):
document.write myVariable
Don't you need to change your loop slightly?
For x = 0 To num - 1
E.G. With 4 items you need to iterate from 0 to 3.