I have no experience with JScript or javascript, but I had an issue that I thought might be a quick solution with what was available online. Ultimately I am trying to create a macro with javascript, but the rest of it is in VBA7. I found a dated article for embedding javascript into wsc for vba: WSC for Excel Macro
Attempting to use visual studio add in projects proved to be beyond my expertise and I was hoping to avoid studying a new language.
I'm getting an error that says "[31,15] Expected ;"
Will this work? Is there a better way to accomplish this? I imagine there might be multiple errors in my code.
<?xml version="1.0"?>
<package>
<component id="RangeDelete">
<comment>
JS delete function
</comment>
<?component error="true" debug="true"?>
<registration
description="WSC Component for cell delete"
progid="RangeDelete"
version="1.00"
remotable="False">
<script language="VBScript">
<![CDATA[
strComponent = "Deleting Range of cells"
Function Register
MsgBox strComponent & " - registered."
End Function
Function Unregister
MsgBox strComponent & " - unregistered."
End Function
]]>
</script>
</registration>
<public>
<method name="DeleteCells">
</method>
</public>
<script language="Javascript">
<![CDATA[
await run(async (context) => //here is where I get the error "[31,15] Expected ;"
let range = context.workbook.getSelectedRange();
range.delete(Excel.DeleteShiftDirection.up);
await context.sync();
});
]]>
</script>
</component>
</package>
Related
I try to implement a sidebar on my spreadsheet to get user input for my scripts to use. I haven't been able to get it to successfully call any server side functions. I put together a simple script from the google documentation and several stackoverflow questions that I read through, but I keep getting an error. It is able to print to the console, but it errors out trying to call the logText() function with google.script.run.
Script File:
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Extra Functions')
.addItem('Show sidebar', 'showSidebar')
.addToUi();
}
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('Test')
.setTitle('Testing')
.setWidth(300);
SpreadsheetApp.getUi()
.showSidebar(html);
}
function logInput(text) {
Logger.log(text);
}
HTML File (Test.html):
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function onFailure(error) {
var div = document.getElementById('output');
div.innerHTML = "ERROR: " + error.name + ": " + error.message;
}
function logText(){
var txt = document.getElementById("txt_input").value;
console.log(txt);
google.script.run.withFailureHandler(onFailure).logInput(txt);
}
</script>
</head>
<body>
<label for="txt_input">Input Text:</label>
<input type="text" id="txt_input"><br>
<button onclick='logText()'>Send Name</button><br>
<div id="output"></div>
</body>
</html>
I've tried running it both on the new Apps Script V8 and Apps Script Legacy, and I get a slightly different error on each.
Apps Script Legacy
ERROR: ScriptError: You do not have access to perform that action. Please ask the owner of this item to grant access to you.
Apps Script V8
ERROR: ScriptError: We're sorry, a server error occurred while reading from storage. Error code PERMISSION_DENIED.
I've been doing research on Authorization but as far as I can tell, it has all the permissions it needs as a Container-Bound Script (https://developers.google.com/apps-script/guides/bound). It has the /auth/script.container.ui OAuth Scope which should allow it to "Display and run third-party web content in prompts and sidebars inside Google applications", as well as the /auth/spreadsheets Scope. I am also the owner of the spreadsheet and the script project.
Since it is not functioning as a Web App it does not need to be deployed, and does not need a doGet() function.
https://developers.google.com/apps-script/guides/html#serve_html_as_a_google_docs_sheets_slides_or_forms_user_interface
I've had the same issue, for me the problem was having a user being logged in with multiple google accounts. It might apply to your case as well. The user tried logging-in with just one account and the permission issue was gone.
It's the same problem that might occur when installing a custom addon.
Hope it helps.
Seems like a bug in the new engine. I have a similar problem. A function from the script is invoked from the HTML. The new engine fails. I disabled the V8 engine and it worked so it seems to be something internal to Google.
I experienced exactly the same problem and solved it disableling the new V8 engine.
I'd an applet "edu.MGT.MainApplet" implemented in a jar "MGT2-0.0.1.jar". A local html page used the tag <applet> to call methods of this class. Everything was working correctly some time ago.
(In detail: the applet provides a set of methods for analysis and transformation of mathematical expression. Mathematical expressions are stored as data trees, and the set of methods allows apply to them usual math operations, finding sub-expressions, execute calculus, ... . The API of the applet is very simple: methods have a name and simple arguments, usually only one integer or one string and returns one string. The trees with the math expressions are never transferred, they are keep at applet. The html/javascript GUI contains buttons to execute the methods and transforms the string result, using MathJax, to graphical mathematical expressions).
The related javascript lines are:
> <applet id="mgt"
> archive="MGT2-0.0.1.jar" style="width: 1px; height: 1px; float: left;"
> code="edu.MGT.MainApplet">
> </applet>
>
> <script>
> var TeX = mgt.predefined(1);
> ...
where "mgt.predefined(1)" is one example of a call to one method of the java class that is the applet.
Nowadays, with navigator firefox 57.0, the console shows the error:
TypeError: mgt.predefined is not a function [Learn More]
gtexpression1.html:90:15
I've checked the content of the jar. Class exists and method exists:
$ jar -xvf MGT2-0.0.1.jar
...
inflated: edu/MGT/MainApplet.class
...
and the class has the related method:
$ javap edu/MGT/MainApplet.class
Compiled from "MainApplet.java"
public class edu.MGT.MainApplet extends java.applet.Applet {
...
public java.lang.String predefined(int);
After lots of hours googling and trying ...
Try number 1:
use <object> instead of <applet>: no changes in the result.
Try number 2:
Move to Java Web Start. I've wrote following MGT2.jnlp file:
<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="" href="">
<information>
<title>MGT</title>
<vendor>private</vendor>
</information>
<resources>
<j2se version="1.7+"
href="http://java.sun.com/products/autodl/j2se" />
<jar href="MGT2-0.0.1.jar" main="true" />
</resources>
<applet-desc main-class="edu.MGT.MainApplet"
name="MGT"
width="1" height="1">
</applet-desc>
<update check="background"/>
</jnlp>
and changed the javascript part to:
<script src="http://www.java.com/js/deployJava.js"></script>
<script>
var attributes = {
code: "edu.MGT.MainApplet",
id: "MGT",
width:1, height:1} ;
var parameters = {jnlp_href: 'MGT2.jnlp'} ;
deployJava.runApplet(attributes, parameters, '1.7');
but following error appears:
ReferenceError: mgt is not defined [Learn More]
gtexpression1.html:103:9
Try number 3:
Same jnlp file than in previous, one more line in the javascript code:
<script src="http://www.java.com/js/deployJava.js"></script>
<script>
var attributes = {
code: "edu.MGT.MainApplet",
id: "mgt",
width:1, height:1} ;
var parameters = {jnlp_href: 'MGT2.jnlp'} ;
deployJava.runApplet(attributes, parameters, '1.7');
var mgt = document.getElementById("MGT");
and the error is:
TypeError: mgt is null [Learn More] gtexpression1.html:103:9
Nowadays, how to call Java code from www page ?
the <applet> tag has been obsolete for several years now, and definitely does nothing in any recent version of Firefox. So the simplest answer here would be that if you really are using firefox (you say you're using a firefox user agent, so it's unclear whether you mean you're using firefox, or you've change the useragent string to claim your non-firefox browser is firefox), the applet is never loaded, so your JS is trying to call something that doesn't exist as a function, and that's not going to work.
At the very least, you want to try to load your code with <object> instead (and then you'll need to have an applet execution plugin installed, FF/Chrome no longer execute java themselves), but an even better suggestion would be to not use Java at all, as modern JS has obviated the need for Java integration into pages.
Hello,
I am trying to test error supression on Sharepoint but I am having some trouble.
This is my process:
On a relatively plain website (all it contains is a colored-in div), I added this script:
<script>
var x[] = 0;
var err = 10/x;
alert(err);
</script>
When setting my Outlook homepage to this site, I see this error:
I also have the following script, which suppresses this message (when adding this to my code, the error message doesn't appear):
<script type="text/javascript">
window.onerror = function(message, url, lineNumber) {
// code to execute on an error
return true; // prevents browser error messages
};
</script>
I want to test this script out on my Sharepoint site, but when I embed the above, error-inducing code onto my Sharepoint homepage and open the page in Outlook, I am not seeing any error messages.
I added the code in the following ways:
1 - Page > Edit > Edit Source > Added the code to the top
2 - Page > Edit > Embed Code > Added the code to various areas of the page
Neither of these methods worked, and the first one actually produced a message telling me that I should use the embed function, which also doesn't seem to work!
I need to generate this error from the Sharepoint page so that I can check **whether the error-suppressing script actually does what it's supposed to. Can **anyone think of what may be going wrong here?
Any help is much appreciated!
This is apparently a known issue in Sharepoint, and can be resolved by using the following function:
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
//your code goes here...
});
For the purposes of the above test, I was able to generate an error with the following:
<script language='javascript'>
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
var x[] = 0;
var err = 10/x;
alert(err);
});
</script>
And adding the below suppressed the errors:
<script language='javascript'>
window.onerror = function(message, url, lineNumber) {
return true;
};
</script>
Note that this only worked for me after adding the 2 bits of code in their own, seperate <script> tags. I also had to add language='javascript' to the tags before it would work.
I added the code by embedding some new code, and adding both of the script tags to that web part. Because I was able to produce the error message, I was also able to prove that the error-suppression method worked.
I would like to save the results calculated on html page in a textfile using javascript.
<script type="text/javascript">
window.onload = function () {
var sw : StreamWriter = new StreamWriter("HTML_Results.txt");
sr.Write('xyz");
*** calculations ******
sr.Write (result);
}
</script>
by doing this, my WP8 App is misbehaving and not displaying images as usual. This app is an Image Fader (calculates FPS).
Also tried:
StreamWriter sr;
try {
sr = new StreamWriter("\HTML5\HTMLResults.txt");
sr.Write("xyz");
File.SetAttributes("HTML5\HTMLResults.txt", FileAttributes.Hidden);
} catch(IOException ex) {
console.write ("error writing"); //handling IO
}
The aim is to:
Extract calculated values of several html pages(after getting loaded
one by one) in a single text file.
A Resultant HTML that reads this
text file and displays results in a tabular format.
Is there a better way to this job or the above can be rectified and used? Appreciate help.
Perhaps I've misunderstood your code but it looks like you're trying to write Java within JavaScript scripting tags. You cannot write Java in an HTML document. As far as I know, client-side JavaScript (which given your <script> tags is I guess what you're trying to write) can't perform the kind of file I/O operations you seem to want here.
You need to use Node JS to use JavaScript for something like that and then you're talking server-side. The closest you can get on client-side is using the new localStorage feature in HTML5 (not supported by all browsers).
I have used HTTRACK to download Federal regulations from a government website, and the resulting HTML files are not intuitively named. Each file has a <TITLE></TITLE> tag set, that would serve nicely to name each file in a fashion that will lend itself to ebook creation. I want to turn these regulations into an ebook for my Kindle, so that I can have the regulations readily available for reference, rather than having to carry volumes of books with me everywhere.
My preferred text/hex editor, UltraEdit Professional 15.20.0.1026, has scripting commands enable through embedding of the JavaScript engine. In researching possible solutions to my problem, I found xmlTitleSave on the IDM UltraEdit website.
// ----------------------------------------------------------------------------
// Script Name: xmlTitleSave.js
// Creation Date: 2008-06-09
// Last Modified:
// Copyright: none
// Purpose: find the <title> value in an XML document, then saves the file as the
// title.xml in a user-specified directory
// ----------------------------------------------------------------------------
//Some variables we need
var regex = "<title>(.*)</title>" //Perl regular expression to find title string
var file_path = UltraEdit.getString("Path to save file at? !! MUST PRE EXIST !!",1);
// Start at the beginning of the file
UltraEdit.activeDocument.top();
UltraEdit.activeDocument.unicodeToASCII();
// Turn on regular expressions
UltraEdit.activeDocument.findReplace.regExp = true;
// Find it
UltraEdit.activeDocument.findReplace.find(regex);
// Load it into a selection
var titl = UltraEdit.activeDocument.selection;
// Javascript function 'match' will match the regex within the javascript engine
// so we can extract the actual title via array
t = titl.match(regex);
// 't' is an array of the match from 'titl' based on the var 'regex'
// the 2nd value of the array gives us what we need... then append '.xml'
saveTitle = t[1]+".xml";
UltraEdit.saveAs(file_path + saveTitle);
// Uncomment for debugging
// UltraEdit.outputWindow.write("titl = " + titl);
// UltraEdit.outputWindow.write("t = " + t);
My question is two-fold:
Can this JavaScript be modified to extract the <TITLE></TITLE> contents from an HTML file and rename the files?
If the JavaScript cannot be modified easily, is there a script/program/black magic/animal sacrifice that can accomplish the same thing?
EDIT:
I have been able to get the script to work as desired by removing the UltraEdit.activeDocument.unicodeToASCII(); line and changing the file extension to .html. My only issue now is that while this script works on single open files, it does not batch process the directory.
You can use just about any "scriptable" language to do something like this pretty quickly. Ruby is my favorite:
require 'fileutils'
dir = "/your/directory"
files = Dir["#{dir}/*.html"]
files.each do |file|
html = IO.read file
title = $1 if html.match /<title>([^<]+)<\/title>/i
FileUtils.mv file "#{dir}/#{title}.html"
puts "Renamed #{file} to #{title}.html."
end
Obviously if your UltraEdit script worked for you this might be obtuse, but for anybody running a different env, hopefully this is useful.
Does this not work out of the box?
I don't know anything about UltraEdit, but as far as a regex engine is concerned, if it can parse <title>(.*)</title> out of an XML document, it can do the exact same for HTML.
Just modify the final file title to .html instead of .xml
saveTitle = t[1]+".html";
Assuming you can get that script to work as it's intended (point being I don't know UltraEdit), I'm pretty confident that same process will work for HTML.
XML and HTML are both plain text, and that script is simply running a regular expression on the text to extract the title tags, which are the same in both; the only thing you need to do is change this line:
saveTitle = t[1]+".xml";
to this:
saveTitle = t[1]+".html";
After much searching and trial and error on the scripting side, I ran across a fantastic program for Windows that will do the renaming via TITLE tags: Flexible Renamer 8.3. The author's website is http://hp.vector.co.jp/authors/VA014830/english/FlexRena/, and it manages to handle every bit of what I needed. Many thanks to #coreyward and #Yuji for their fantastic advice on the scripting end of things.