Read xml from url using js - javascript

I have a Google Map that reads a XML file local on my server to parse data on a map. Works great. I would like to use a URL straight to the data instead of using WGET to save it to my server. How can I go about using a URL instead?
Currently this is how I set the file path to the WGET file that I am pulling from the URL I want to use directly.
var xml = 'xml/killer-tornados2014.xml';
Which the XML file is then read as so. As stated, works great but would like to pull directly from the XML URL for simplicity.
downloadUrl(xml, function(data) {
var markers = data.documentElement.getElementsByTagName("fatalities");
for (var i = 0; i < markers.length; i++) {
var yrnum = markers[i].getAttribute("yrnum");
var dt = markers[i].getAttribute("dt");
var tm = markers[i].getAttribute("time");
var ef = markers[i].getAttribute("ef");
var st = markers[i].getAttribute("st");
var loc = markers[i].getAttribute("location");
var watch = markers[i].getAttribute("watch");
var dead = markers[i].getAttribute("deaths");
var h = markers[i].getAttribute("h");
var m = markers[i].getAttribute("m");
var o = markers[i].getAttribute("o");
var v = markers[i].getAttribute("v");
var p = markers[i].getAttribute("p");
var unk = markers[i].getAttribute("unk");

Related

Indesign Script for PDF Object Layer Options

I have an indesign document with multiple pages. each page has a linked pdf in it. each pdf has 3 layers within it and in order to turn these layers on or off, you have to right click, select Object Layer Options, and then manually turn on or off layers.
I would like to loop through all my pages and turn on a layer in the PDF using a script. i have been messing with graphicLayerOptions.graphicLayers but keep running into an error when telling it to turn the currentVisibilty=true;
var myDocument = app.activeDocument;
var docLength = myDocument.pages.length;
var myPages = myDocument.pages
for (var i = 0; i < docLength; i++) {
var labelPlaceholder = myDocument.allGraphics;
var labelArtwork = labelPlaceholder[0];
var artworkLayers = labelArtwork.graphicLayerOptions.graphicLayers;
artworkLayers.item("Die Copy").currentVisibility = true;
}
i got it working...l
var myDocument = app.activeDocument;
var docLength = myDocument.pages.length;
var myPages = myDocument.pages
for (var i = 0; i < docLength; i++) {
var labelPlaceholder = myPages[i].allGraphics;
var labelArtwork = labelPlaceholder[0];
var artworkLayers = labelArtwork.graphicLayerOptions.graphicLayers;
artworkLayers[0].currentVisibility = true;
}
Just in case. In InDesing (Illustrator, etc) you have two options to get an item from a collection.
By its number:
var layer = app.activeDocument.layers[0];
By its name:
var layer = app.activeDocument.layers.itemByName("Die Copy");
Later options is less reliable. Not all collections has this method. I don't know if it (PDF layers) is the case, though.

error : "Cannot convert Array to Object" Data Scraping script

I'm new to Javascript and even newer to google script, so please be comprehensive :)
I'm trying to build a little script to scrap some data from a bunch of URL. I' using Parser library. Here is what I've:
function getArray() {
var newData = new Array();
var sheet = SpreadsheetApp.openById('my_id').getSheetByName('Sheet4');
var urls = sheet.getRange(1,1,5,5).getValues();
var fromText = '<span class="nb-shares">';
var toText = '</span>';
for(i in urls){
var url = urls[i];
var content = UrlFetchApp.fetch(url).getContentText();
var scraped = Parser
.data(content)
.from(fromText)
.to(toText)
.build();
newData.push(scraped);}
var sheet2 = SpreadsheetApp.openById('my_id').getSheetByName('Sheet5');
sheet2.getRange(5, 1, newData.length, newData[1].length).setValues(newData);
}
It return me the following error : Cannot convert Array to Object
What I'm trying to do is looping on an URLs array so I can scrap some data from each one of these URL and return the results in my sheet.
Try changing newData.push(scraped) to newData.push([scraped])

how can I use second sheet when I save/read csv file in javascript?

// save to CSV
var csv = document.getElementById('a');
var csvContent = new Array();
csvContent.push(["COL_A","COL_B","COL_C"]);
var fileName = "file.csv";
var blob = new Blob(["\ufeff", csvContent]);
var url = URL.createObjectURL(blob);
csv.href = url;
csv.setAttribute('download', fileName);
csv.click();
// load CSV file
..
var lines = loadedCsvFile.split(/\r\n|\n/);
var result = [];
var headers = lines[0].split(",");
for(var i=0;i<lines.length;i++){
var obj = {};
var currentLine = lines[i].split(",");
for(var j=0;j<headers.length;j++){
obj[headers[j]] = currentLine[j];
}
result.push(obj);
}
this is my csv save/load code.
But, this code can use only first sheet.
how can I use second sheet?
I should be very grateful to you if you might help me.
I assume you're talking about a CSV from Excel (or the likes) which 'had' multiple sheets?
CSVs don't use the sheet concept, they're are saved as a flat file. You will either need to save each sheet as a CSV individually and read separately, or concatenate the sheets.

Parse all values from a XML element using Google Apps Script?

I am trying to parse forex values (all of them) for http://indicador.eof.cl/rss XML feed into a Gooogle Sites trough Google Apps Script.
The script as follow>
function doGet(){
var response = UrlFetchApp.fetch("http://indicador.eof.cl/rss").getContentText();
var parsedResponse = Xml.parse(response, false);
var root = parsedResponse.getElement();
var entries = root.getElement('channel').getElements("item");
for (var i=0; i<entries.length; i++) {
var e = entries[i];
var title = e.getElement("title").getText();
var description = e.getElement("description").getText();
}
var app = UiApp.createApplication();
var TopVar = app.createHorizontalPanel();
TopVar.add(app.createLabel(title).setStyleAttribute("fontSize","12px"));
TopVar.add(app.createLabel(description).setStyleAttribute("fontSize","12px"));
app.add(TopVar);
return app;
}
The issue is the code just bring me the first value no all of them, what i am forgetting?
Best Regards,
Try to move TopVar.add(...); lines inside for loop :
var app = UiApp.createApplication();
var TopVar = app.createHorizontalPanel();
for (var i=0; i<entries.length; i++) {
var e = entries[i];
var title = e.getElement("title").getText();
var description = e.getElement("description").getText();
TopVar.add(app.createLabel(title).setStyleAttribute("fontSize","12px"));
TopVar.add(app.createLabel(description).setStyleAttribute("fontSize","12px"));
}
Actually, I know nothing about google-apps-script. But your current code logic seems a bit off. It doesn't make use of values of local variables declare inside for loop (e, title, and description). Value of those variables changed in every iteration without any code using it.

Photoshop Scripting array to json

I want to get all the layers in a json document.
Here is my code :
#include json2.js
var doc = app.activeDocument;
var allLayers = [];
var allLayers = collectAllLayers(doc, allLayers);
function collectAllLayers (doc, allLayers){
for (var m = 0; m < doc.layers.length; m++){
var theLayer = doc.layers[m];
if (theLayer.typename === "ArtLayer"){
allLayers.push(theLayer);
}else{
collectAllLayers(theLayer, allLayers);
}
}
return allLayers;
}
var json = JSON.stringify(allLayers);
alert(json);
My efforts var json = JSON.stringify(allLayers); doesn't working. I want to allLayers change to json.
Thanks for help answers in advance!
Some of the types Photoshop uses aren't supported by JSON (like File for instance), so JSONing some of the Photoshop DOM objects won't work. You'll need to modify json2.js or create your own parser that'll create a jsonable object.

Categories

Resources