google sheets apps script - debugging

I wanted to ask I have a few lines of code. And until yesterday, everything worked perfectly for me.
Now when I start debugging the script, it ends up with an error message on line 24 => var ui = SpreadsheetApp.getUi();.
Exception: Cannot call SpreadsheetApp.getUi() from this context.
inputBox # ulozDataDoDb.gs:24
But when I run this script from the button in the google sheet, it works.
Does anyone know what to do with it?
function inputBox() {
//definice aktivního sešitu
var ss = SpreadsheetApp.getActive();
//definice aktivního listu
var faktura = ss.getSheetByName("faktura");
var dataBaze = ss.getSheetByName("db");
//definice promenných pro dalši práci
var cisloFaktury = faktura.getRange("I3").getValue();
var varSymbol = faktura.getRange("E22").getValue();
var datumVystaveni = faktura.getRange("datumVystaveni").getValue();
var prijmeni = faktura.getRange("prijmeni").getValue();
var jmeno = faktura.getRange("jmeno").getValue();
var prijmeniJmeno = jmeno + " " + prijmeni;
var castka = faktura.getRange("I55").getValue();
var konstSymb = faktura.getRange("E23").getValue();
var specSymb = faktura.getRange("E24").getValue();
var formaUhrady = faktura.getRange("E19").getValue();
//zapsat do DB
//popis do DB
var ui = SpreadsheetApp.getUi();
var popis_v_DB = ui.prompt("Zadejte název do DB.").getResponseText();
popis_v_DB = popis_v_DB.split(" ").join("_"); //vyhledá mezery a nahradí znakem

The Cannot call SpreadsheetApp.getUi() from this context. exception you are receiving is due to the fact that that prompt has to be actioned on by a user and not by a script.
According to the documentation:
prompt(prompt) - Will open an input dialog box in the user's editor with the given message and an "OK" button. This method suspends the server-side script while the dialog is open. The script resumes after the user dismisses the dialog.
Reference
Apps Script Ui Class - prompt().

Try to replace
var ui = SpreadsheetApp.getUi();
var popis_v_DB = ui.prompt("Zadejte název do DB.").getResponseText();
by
var popis_v_DB = Browser.inputBox("Informace.", "Zadejte název do DB.", Browser.Buttons.OK_CANCEL);
but as far as you ask somebody to enter a value, you will do it by the sheet not by the script editor !

Related

How to read a blob data URL in WKWebView?

I have a WKWebView, but when I click on a link that has the following URL:
blob:https://www.mycase.com/2963c6c0-24c1-418f-a314-88f7a2dbc713
Nothing happens. Reading the documentation it describes:
The only way to read content from a Blob is to use a FileReader.
So I presume there is no way WKWebView is able to read the Blob itself by some method. As URLSession fails to download the content when you feed it the blob URL.
A solution I came up with myself is to read the blob in JavaScript itself.
var script = ""
script = script + "var xhr = new XMLHttpRequest();"
script = script + "xhr.open('GET', '\(navigationURL)', true);"
script = script + "xhr.responseType = 'blob';"
script = script + "xhr.onload = function(e) { if (this.status == 200) { var blob = this.response; var reader = new window.FileReader(); reader.readAsBinaryString(blob); reader.onloadend = function() { window.webkit.messageHandlers.readBlob.postMessage(reader.result); }}};"
script = script + "xhr.send();"
self.webView.evaluateJavaScript(script, completionHandler: nil);
Then add a script message handler to fetch the content.
let configuration = WKWebViewConfiguration()
configuration.userContentController.add(self, name: "readBlob")
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
print(message.body)
}
It feels a bit cumbersome to do it like this, is there any other way?

Applescript: Getting list of selected layers on Photoshop

I am creating an AppleScript where I need to do something to the selected layers on Photoshop.
How do I get the list of the selected layers on Photoshop even if the selected layers are inside groups?
I don't have code to show right now because it all starts by having the list of selected layers, sorry.
Selected layers is not a property in JavaScript's artLayer object and selected is not an property of the layer object in AppleScript either. However we can work with AM in PhotoShop and use actions and it's descriptor result to get the selected layers. Because the layers may need to swift depending on whether there is an background layer or not we first create an array with selected indices (code is based on this post) and after that we resolve the names of the layers.
tell application "Adobe Photoshop CS6"
tell document 1
set selectedLayers to paragraphs of (do javascript "
var typeDocument = stringIDToTypeID('document');
var typeItemIndex = stringIDToTypeID('itemIndex');
var typeLayer = stringIDToTypeID('layer');
var typeName = stringIDToTypeID('name');
var typeOrdinal = stringIDToTypeID('ordinal');
var typeProperty = stringIDToTypeID('property');
var typeTarget = stringIDToTypeID('targetEnum');
var typeTargetLayers = stringIDToTypeID('targetLayers');
var selectedLayers = new Array();
var actionRef = new ActionReference();
actionRef.putEnumerated(typeDocument, typeOrdinal, typeTarget);
var actionDesc = executeActionGet(actionRef);
if(actionDesc.hasKey(typeTargetLayers) ){
actionDesc = actionDesc.getList(typeTargetLayers);
var c = actionDesc.count
for(var i=0;i<c;i++){
try{
activeDocument.backgroundLayer;
selectedLayers.push(actionDesc.getReference( i ).getIndex() );
}catch(e){
selectedLayers.push(actionDesc.getReference( i ).getIndex()+1 );
}
}
}else{
var actionRef = new ActionReference();
actionRef.putProperty(typeProperty , typeItemIndex);
actionRef.putEnumerated(typeLayer, typeOrdinal, typeTarget);
try{
activeDocument.backgroundLayer;
selectedLayers.push( executeActionGet(actionRef).getInteger(typeItemIndex)-1);
}catch(e){
selectedLayers.push( executeActionGet(actionRef).getInteger(typeItemIndex));
}
}
var selectedLayerNames = new Array();
for (var a in selectedLayers){
var ref = new ActionReference();
ref.putIndex(typeLayer, Number(selectedLayers[a]) );
var layerName = executeActionGet(ref).getString(typeName);
selectedLayerNames.push(layerName);
}
selectedLayerNames.join('\\n');
")
end tell
end tell

Cannot update label on Google Apps Script GUI Builder Interface at runtime

I have an interface that calls a script for spreadsheet creation using data taken from other spreadsheet. I want the interface to update its labels at runtime in order to give visual feedback to the user and let him know the script is running and it's not stuck. When I try to update the label I put in the interface, it doesn't update the first time, but updates correctly after myFunction() reaches its end. Which means I can see the message "Creation Completed", but the message "Creating file..." is never shown. Also, the button buttonCompile is never disabled so it seems that the instructions before myFunction() are not executed at all. How can I get the labels updated and the button disabled before myFunction() starts executing? (I already double-checked variable references)
function doGet() {
var app = UiApp.createApplication();
app.add(app.loadComponent("File creation"));
var buttonCreate = app.getElementById('createBtn');
var handlerCrea = app.createServerHandler('createClickHandler');
buttonCreate.addClickHandler(handlerCreate);
return app;
}
function createClickHandler(e) {
var app = UiApp.getActiveApplication();
var label = app.getElementById('createLbl');
label.setText("Creating file...");
var buttonCompile = app.getElementById('compileBtn');
buttonCompile.setEnabled(false);
myFunction();
label.setText("Creation completed.");
buttonCompile.setEnabled(true);
app.close();
return app;
}
The cause of this behavior is that the GUI is updated only after leaving a handler. A workaround is to use two handlers. The 1st one sets the label text to Creating file... and disables the button, the 2nd one executes the myFunction function, changes the text to Creation completed, and eanbles the button. Here is an example. It disables/enables the button and the worker handler simply waits 5 seconds.
function doGet(e) {
var app = UiApp.createApplication();
var container = app.createHorizontalPanel().setId('container');
var btnPerformance = app.createButton("Performance Demo").setId('btnPerformance');
var handlerPerformance = app.createServerHandler('onBtnPerformanceClick');
var handlerWait = app.createServerHandler('onWait');
btnPerformance.addClickHandler(handlerPerformance);
btnPerformance.addClickHandler(handlerWait);
container.add(btnPerformance);
app.add(container);
return app;
}
function enableControls(enable) {
var lstControls = [ 'btnPerformance' ];
var app = UiApp.getActiveApplication();
for (var i = 0; i < lstControls.length; i++) {
var ctl = app.getElementById(lstControls[i]);
ctl.setEnabled(enable);
}
}
function onWait(e) {
enableControls(false);
return UiApp.getActiveApplication();
}
function onBtnPerformanceClick(e) {
Utilities.sleep(5000);
enableControls(true);
return UiApp.getActiveApplication();
}

Store & retrieve the identifiers of a multipliable widget's instances

The aim is to remove only the last row at any time and only by the last remove button.
There is a user interface which building up as a multiplication of the same row. The number of rows are controlled by 'Add' & 'Remove' buttons which are also elements of the row. The problem is that the hidden widgets - that are applied for each row to distinguish the instances by storing their row numbers - are storing the very same number which is the last one. Except the first (0) hidden widget which stores the proper number (0). Where am I missing the point? How should this be resolved?
As per the remove buttons have two different purposes (not detailed here), we use a cacheService to distinguish the last row from all the others. Only the last row should be removed at any time.
var cache = CacheService.getPrivateCache();
we clear the cache and create the first instance
function doGet() {
var app = UiApp.createApplication();
app.add(app.createVerticalPanel().setId('mainContainer'));
cache.removeAll([]);
ui(0);
cache.put('numberOfInstances',0);
return app; }
each instance is held by a horizontal panel which contains the mentioned hidden widget, a label which informs about the instance number, and the Add & Remove buttons.
function ui(instance) {
var app = UiApp.getActiveApplication();
var eventContainer = app.createHorizontalPanel()
.setId('eventContainer' + instance);
var instanceContainer = app.createHidden('instanceContainer',instance);
var showInstance = app.createLabel(instance)
.setId('showInstance' + instance);
var addButton = app.createButton('Add')
.setId('add' + instance)
.addClickHandler(app.createClientHandler()
.forEventSource().setEnabled(false)) //avoiding multiple click during server response
.addClickHandler(app.createServerHandler('add')
.addCallbackElement(instanceContainer));
var removeButton = app.createButton('X')
.addClickHandler(app.createServerHandler('remove')
.addCallbackElement(instanceContainer));
app.getElementById('mainContainer')
.add(eventContainer
.add(instanceContainer)
.add(showInstance)
.add(addButton)
.add(removeButton));
return app; }
and the event handling...
function add(inst) {
var app = UiApp.getActiveApplication();
var instance = Number(inst.parameter.instanceContainer);
ui(instance+1);
cache.put('numberOfInstances',instance+1);
return app; }
function remove(inst) {
var app = UiApp.getActiveApplication();
var instance = Number(inst.parameter.instanceContainer);
var numberOfInstances = cache.get('numberOfInstances')
if( (instance != 0) && (instance = numberOfInstances) ) {
app.getElementById('mainContainer').remove(app.getElementById('eventContainer' + instance));
cache.put('numberOfInstances',instance-1);
app.getElementById('add' + (instance-1)).setEnabled(true); } //avoiding multiple click during server response
return app; }
The aim is to remove only the last row at any time and only by the last remove button.
Many Thanks.
Why don't you simply use a clientHandler just as you did on the 'add' button? You could target the preceding 'remove' button and disable it each time you create a new one and change /update each time you remove one row.
EDIT : I can suggest you something, feel free to have a look, I changed a bit the approach but it is working and I hope you'll find it at least interesting ;-)
Link to the online test
function doGet() {
var app = UiApp.createApplication();
var counter = app.createHidden().setName('counter').setId('counter').setValue('1');
var mainContainer = app.createVerticalPanel().setId('mainContainer')
app.add(mainContainer.add(counter));
var event1Container = app.createHorizontalPanel()
var showInstance = app.createLabel('1')
var addButton = app.createButton('Add')
.setId('add1')
.addClickHandler(app.createClientHandler()
.forEventSource().setEnabled(false)) //avoiding multiple click during server response
.addClickHandler(app.createServerHandler('add')
.addCallbackElement(mainContainer));
var removeButton = app.createButton('X')
.setId('remove1')
.addClickHandler(app.createServerHandler('remove')
.addCallbackElement(mainContainer));
mainContainer.add(event1Container
.add(showInstance)
.add(addButton)
.add(removeButton));
return app; }
function add(inst) {
var app = UiApp.getActiveApplication();
var hiddenVal =inst.parameter.counter;
var counterVal = Number(hiddenVal);
var mainContainer = app.getElementById('mainContainer')
var counter = app.getElementById('counter')
++ counterVal
counter.setValue(counterVal.toString())
var eventContainer = app.createHorizontalPanel().setId('eventContainer'+counterVal)
var showInstance = app.createLabel(counterVal.toString())
var addButton = app.createButton('Add')
.setId('add'+counterVal)
.addClickHandler(app.createClientHandler()
.forEventSource().setEnabled(false)) //avoiding multiple click during server response
.addClickHandler(app.createServerHandler('add')
.addCallbackElement(mainContainer));
var removeButton = app.createButton('X')
.setId('remove'+counterVal)
.addClickHandler(app.createServerHandler('remove')
.addCallbackElement(mainContainer));
app.add(eventContainer
.add(showInstance)
.add(addButton)
.add(removeButton));
if(counterVal>1){app.getElementById('remove'+(counterVal-1)).setEnabled(false)}
return app; }
function remove(inst) {
var app = UiApp.getActiveApplication();
var counterVal = Number(inst.parameter.counter);
var counter = app.getElementById('counter')
if(counterVal ==1) {return app}
var maincontainer = app.getElementById('mainContainer')
app.getElementById('eventContainer' + counterVal).setVisible(false)
--counterVal
counter.setValue(counterVal.toString())
app.getElementById('add'+counterVal).setEnabled(true)
app.getElementById('remove'+counterVal).setEnabled(true)
return app;
}
NOTE : I didn't make use of .remove(widget) since this is a fairly new method and I don't know exactly how it works... I'll test it later. Until then I used setVisible(false) instead, sorry about that :-)
Note 2 : I didn't use the cache since the hidden widget is sufficient to keep track of what is going on... if you needed it for something else then you could always add it back .

AS3 URLRequest in for Loop problem

I read some data from a xml file, everything works great besides urls. I can't figure what's the problem with the "navigateURL" function or with the eventListener... on which square I click it opens the last url from the xml file
for(var i:Number = 0; i <= gamesInput.game.length() -1; i++)
{
var square:square_mc = new square_mc();
//xml values
var tGame_name:String = gamesInput.game.name.text()[i];//game name
var tGame_id:Number = gamesInput.children()[i].attributes()[2].toXMLString();//game id
var tGame_thumbnail:String = thumbPath + gamesInput.game.thumbnail.text()[i];//thumb path
var tGame_url:String = gamesInput.game.url.text()[i];//game url
addChild(square);
square.tgname_txt.text = tGame_name;
square.tgurl_txt.text = tGame_url;
//load & attach game thumb
var getThumb:URLRequest = new URLRequest(tGame_thumbnail);
var loadThumb:Loader = new Loader();
loadThumb.load(getThumb);
square.addChild(loadThumb);
//
square.y = squareY;
square.x = squareX;
squareX += square.width + 10;
square.buttonMode = true;
square.addEventListener(MouseEvent.CLICK, navigateURL);
}
function navigateURL(event:MouseEvent):void
{
var url:URLRequest = new URLRequest(tGame_url);
navigateToURL(url, "_blank");
trace(tGame_url);
}
Many thanks!
In navigateURL() you use tGame_url, but I think you'd rather use something like tgurl_txt.text which will be different for each square.
Looks like you're not attaching the event listener properly. Instead of this.addEventListener, attach it to the variable you created when creating new square_mc..... so:
square.addEventListener(MouseEvent.CLICK, navigateURL);
you should add the addEventListener on the Squares
mmm..still figuring how eventhandler function will ever get the correct tgame_url var.
What if you try this:
square.addEventListener(MouseEvent.CLICK, function navigateURL(event:MouseEvent):void
{
var url:URLRequest = new URLRequest(tGame_url);
navigateToURL(url, "_blank");
trace(tGame_url);
});
try tracing this:
function navigateURL(event:MouseEvent):void
{
var url:URLRequest = new URLRequest(tGame_url);
navigateToURL(url, "_blank");
//trace(tGame_url);
trace(event.currentTarget.tgurl_txt.text);
}
you should add the url to your square in the loop
square.theUrl = tGame_url;
in the event listener function you should be able to access it with
event.currentTarget.theUrl;

Resources