"SpreadsheetApp is not defined" when accessing spreadsheet from JavaScript - javascript

I am receiving this error when trying to access a Google spreadsheet from JavaScript.
Uncaught ReferenceError: SpreadsheetApp is not defined
Why is this happening?
<html>
<head>
<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script type='text/javascript'>
function findAllBP() {
var sheet = SpreadsheetApp.getActiveSpreadsheet(); // error
var sid = SpreadsheetApp.openById('ID');
var dd = sid.getSheetByName('Published').getRange('A').getValues();
var name = sheet.getName();
console.log("name of spreadsheet" + name);
}
window.onload = function() {findAllBP();};
</script>
</head>
<body>
</body>
</html>

According to docs (https://developers.google.com/loader/) first you need to load a module and the use it's API:
google.load("search", "1");
But this is not an issue here. API that you are trying to use is a Apps Script API and it is not available on regular websites. It must be used in context of Spreadsheet.
Read more on Apps Script docs: https://developers.google.com/apps-script/

Related

SP.ClientContext.get_current() return error: Cannot read property 'webServerRelativeUrl' of undefined

I have trouble solving the following issue. I am doing JSOM on html with the scripts located in the Document Library.
Here is my code
index.html
<script src="/_layouts/1033/init.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script src="/_layouts/sp.core.js"></script>
<script src="/_layouts/sp.js"></script>
<script src="../includes/jquery.min.js"></script>
<script src="../includes/script.js"></script>
script.js
jQuery(document).ready(function($) { ExecuteOrDelayUntilScriptLoaded(execOperation, "sp.js"); }
function execOperation() {
var clientContext = new SP.ClientContext.get_current(); // Get SPSITEURL
console.log(clientContext)
var list = clientContext.get_web();
}
When I try to call get_current(), it returns the following error
jquery.min.js:2 Uncaught TypeError: Cannot read property 'webServerRelativeUrl' of undefined
at Function.SP.PageContextInfo.get_webServerRelativeUrl (sp.js:2)
at new SP.ClientContext.get_current (sp.js:2)
Possible to help check anything wrong with my code?
May be you need to load SP.ClientContext by SP.SOD.executeFunc method or load additional scripts like here:
https://sharepoint.stackexchange.com/questions/253763/how-to-get-clientcontext-in-sharepointonline-with-jsom
Or use REST API instead of JSOM.

javascript variables loaded from server are not defined in chrome but firefox

After form data input the data will be processed on server. The result will be responded immediately to client on same site where the data were entered. The tool "visualize.js" needs the result to visualize.
view.py:
if form.is_valid():
//data processing ...
return render(request, 'data_input.html',{'n':n,'t':t,'f':f,'s':s})
data_input.html:
<script type='text/javascript'>
var n_value = {{n}}; //to be visualized
var t_value = {{t}}; //to be visualized
var f_value = {{f}}; //to be visualized
var s_value = {{s}}; //to be visualized
</script>
<script type='text/javascript' src ="{% static "visualize.js" %}" ></script>
In firefox the variables can be found and visualized by the external tool "visualize.js" but in chrome they seem not to be defined with the error:
Uncaught ReferenceError: n is not defined
I also tried some code tricks like "visualize.js" should be loaded after the result is there but no success.
Any idea I could try? Thank you.
So, I already mentioned that my javascript visualization tool only worked in firefox and not in google chrome. After I tried a lot I found this solution:
<script type='text/javascript'>
var n_value = {{n|safe}}; //to be visualized
var t_value = {{t|safe}}; //to be visualized
var f_value = {{f|safe}}; //to be visualized
var s_value = {{s|safe}}; //to be visualized
</script>
safe has to be used.

How to tweak this javascript file to read in my data on localhost. parsing RDF task

I'm using this Javascript RDF parser.
In the documentation it says to use it accordingly:
getRDFURL(url,func,followSeeAlso)
Downloads and parses RDF from a url.
url is the url to recieve the RDF from, use the full url, not a relative one, or the base url will be wrong.
func is a javascript function to call when the rdf has been processed.
In the file for the parser, I spied this empty variable:
var baseURL='';
and I filled it up like so:
var baseURL='http://localhost:8888/demo/StackOverflow-Europe.rdf';
In my index.html file I tried to call this parsing script in this way:
<!DOCTYPE html>
<meta charset='utf-8'>
<html>
<head>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<link rel='stylesheet' href='style.css'>
</head>
<body>
<!--
<script type='text/javascript' src='script.js'></script>
-->
<script type='text/javascript' src='parser.js'></script>
</body>
</html>
but finally... nothing happened.
What am I doing wrong?
I guess that's not the right way to call javascript files? Is that it? Or maybe there's another reason.
I'm not so familiar with Javascript.
As Jeen Broekstra said in the comments, the line you use in your html file:
<script type='text/javascript' src='parser.js'></script>
has to only purpose to load the the rdf-parser library.
You can load it directly on the main server
<script src="http://www.jibbering.com/rdf-parser/parser.js"></script>
and use your own script.
You can use the official demo as a starter point:
<script type="text/javascript">
function demo() {
foafNS = "http://xmlns.com/foaf/0.1/";
myRDF = new RDF();
myRDF.getRDFURL('/foaf2.rdf', callback);
function callback() {
alert("http://jibbering.com/foaf2.rdf contains the following triples\n\n" + myRDF.toNTriples())
nm = myRDF.Match(null, null, foafNS + "name", "Jim Ley")
mbox = myRDF.getSingleObject(null, nm[0].subject, foafNS + "mbox", null)
alert("The e-mail address of Jim Ley is " + mbox)
}
}
document.write('<p>See demo using /foaf2.rdf <button onclick="demo()">See Demo</button></p>')
</script>
new RDF() allows you to create an RDF utility object
getRDFURL('/foaf2.rdf', callback) loads the foaf2.rdf file and set the function callback as a callback, i.e. this function is called when the rdf file has been completely loaded.
myRDF.toNTriples() returns all RDF triples.
nm = myRDF.Match(null, null, foafNS + "name","Jim Ley") returns an array of triples that match the subject/predicate/object pattern.
mbox = myRDF.getSingleObject(null, nm[0].subject, foafNS + "mbox", null) returns the value of the object in the collection of triples that matches the subject/predicate/object pattern, or an empty string if not found.

How to use Google API in a js file?

I want to use Google API inside a js file and how can I use it? I tried to use google.load() directly in the js file but was told that google is not defined. Then I tried to use the following code
var s = document.createElement('script');
but was told that document is not defined.
What should I do to use google api inside a js file? Thank you.
Here what I want to use is the Google Feed API.
The html code I used is
<!DOCTYPE HTML>
<html>
<head>
<title>
Example
</title>
<script src="https://www.google.com/jsapi" type="text/javascript"></script>
</head>
<body>
<div>
<p id="computation_results">please wait, computing … </p>
</div>
<script>
var worker = new Worker('numberworker.js');
worker.postMessage({first:123,second:456});
worker.onmessage = function (event)
{
alert(event.data);
document.getElementById('computation_results').textContent = event.data;
};
</script>
</body>
</html>
and the js file where I want to use the api is
// Our callback function, for when a feed is loaded.
function feedLoaded(result) {
if (!result.error) {
var container = document.getElementById("content");
container.innerHTML = '';
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
var div = document.createElement("div");
div.appendChild(document.createTextNode(entry.title));
container.appendChild(div);
}
}
}
function OnLoad() {
var feed = new google.feeds.Feed("http://www.digg.com/rss/index.xml");
feed.load(feedLoaded);
}
onmessage = function (event)
{
var fileref=document.createElement('script');
var filename="https://www.google.com/jsapi";
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", filename);
document.getElementsByTagName("head")[0].appendChild(fileref);
// google.load("feeds", "1");
// google.setOnLoadCallback(OnLoad);
var first=event.data.first;
var second=event.data.second;
postMessage("Work done! "+ " "+first+" "+second);
};
You need to include the API before you can use it. Try putting this in your head block:
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
To load the feed API, you can then have another script block like this:
<script type="text/javascript">
google.load("feeds", "1");
var feed = new google.feeds.Feed("<<url here>>");
...
</script>
I would check to make sure your html is validated first, or put up a link to a test file. Any slightest error can cause you to get any number of errors. I tried the following code in a file by itself, wrapped in tags, and was able to load the api. To load a javascript file using javascript you would use the following
var fileref=document.createElement('script');
var filename="https://www.google.com/jsapi";
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", filename);
document.getElementsByTagName("head")[0].appendChild(fileref);
Then after it you use the api calls
google.load(....)\
EDIT:
You are using html5 web workers, that use process outside of the browser, to create a script. This is not possible due to html5 workers do not have access to the DOM. http://www.sitepoint.com/javascript-threading-html5-web-workers/
You would have to create it outside of the worker or include in a script element

configure log4javascript to struts web application

How can we write the JavaScript info and error messages to the log file using log4j.
Please find the code below
<script type="text/javascript" src="././scripts/log4javascript.js"></script>
<script type="text/javascript">
var log = log4javascript.getDefaultLogger();
log.removeAllAppenders();
log.addAppender("start logging");
</script>
function writetoLog()
{
log.info("Start the Script Log..");
}
<body onload="writetoLog();">Some html code here </body>
This info messages is not write to the my log file.
Please suggest
You need to create an AjaxAppender. It's covered in the documentation of log4javascript (for example, in the quick start tutorial: http://log4javascript.org/docs/quickstart.html).
var log = log4javascript.getLogger("server");
var ajaxAppender = new log4javascript.AjaxAppender("/yourapp/jslog.do");
log.addAppender(ajaxAppender);
log.debug("Testing server log");

Categories

Resources