Google script to automatically detect the cell containing a button, and change the background of that cell? - button

I'm a teacher working on creating a Jeopardy-style game to use in my classroom. I'm very new to Google scripts and coding in general. I'm creating "buttons" for the point values in each category using transparent images and attaching script to change the background color of the cell to grey on click to indicate which point values have already been used, as in this example.
What I would like to do is be able to assign the same exact script to each "button," so that it will automatically detect which cell to change to grey based on which button is pushed. I haven't been able to figure this out, and instead have created a separate script for each button (Category 1, 100 pts; Category 2, 100 pts; Category 3, 100 pts, etc etc). It's complicated and time-consuming, and it just seems like there has to be a better way!
function colorCat1100(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var colors = [
["#e0e0e0"]
];
var cell = sheet.getRange("A3:A3");
cell.setBackgrounds(colors);
}
function colorCat2100(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var colors = [
["#e0e0e0"]
];
var cell = sheet.getRange("B3:B3");
cell.setBackgrounds(colors);
}
function colorCat3100(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var colors = [
["#e0e0e0"]
];
var cell = sheet.getRange("C3:C3");
cell.setBackgrounds(colors);
}
function colorCat4100(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var colors = [
["#e0e0e0"]
];
var cell = sheet.getRange("D3:D3");
cell.setBackgrounds(colors);
}
function colorCat5100(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var colors = [
["#e0e0e0"]
];
var cell = sheet.getRange("E3:E3");
cell.setBackgrounds(colors);
}
...and this is just the first row of points values. I would greatly appreciate any help you wonderful people might have to offer! Thanks in advance!

Issue:
Images can be inserted to a sheet either (1) belonging to a cell (Insert > Image > Image in cell, or using IMAGE), and to which scripts cannot be attached, or (2) not belonging to a specific cell (Insert > Image > Image over cells), to which scripts can be attached.
Assuming you are using Image over cells, you cannot pass parameters to a function that is attached to the image. And considering that images inserted via Image over cells don’t belong to a specific cell, there is no way for the called function to get information on which image has been clicked via getCurrentCell() or getActiveRange().
Workaround: onSelectionChange trigger:
Thanks to the recently added onSelectionChange(e) trigger, you could use the cells themselves as “buttons”: you can run the script when a cell is clicked, instead of attaching the script to over-cell images.
The idea is that, when there is a change on which cell is selected, the script will run and check whether the currently selected cell is one of the ones with "buttons" (this condition is needed to avoid all cells to change background when they're clicked, but only the "button" ones). And if it is, the cell background will be changed.
Just copy and paste the following code:
function onSelectionChange(e) {
var buttonRanges = ["A3", "B3", "C3"]; // Cells with "buttons" (please change)
var range = e.range;
if (buttonRanges.includes(range.getA1Notation())) {
range.setBackground("#e0e0e0");
}
}
Of course, you could add the images themselves to the cells, but instead of using Image over cells, you should add inline cells, either via Image in cell or with =IMAGE.
Note:
In the beginning of this function, the A1 notations of the cells with buttons are defined. In the provided sample, these cells are A3, B3 and C3. Please change according to your preferences.
This will not work if the clicked cell is not the previously selected cell one (only cell selection changes are tracked by this trigger).
Reference:
onSelectionChange(e)

Related

How can I learn pixels band values from image in screen of Earth Engine?

I want to learn pixels band values, for example when I clik on mNDWI image in screen of Earth Engine, I need learning values of red, green and blue
var geometry=ee.Geometry.Polygon([[38.877002459052335,40.75574968156597],
[41.206104021552335,41.17882292442983],
[40.645801287177335,41.59918091806734],
[40.052539568427335,41.84517989453356],
[39.569141130927335,41.886088143011904],
[38.800098162177335,41.48405920501165],
[38.877002459052335,40.75574968156597],
]);
var s2SR = ee.ImageCollection('COPERNICUS/S2_SR')
//filter start and end date
.filter(ee.Filter.calendarRange(2020,2020,'year'))
.filter(ee.Filter.calendarRange(8,8,'month'))
//filter according to drawn boundary
.filterBounds(geometry)
.filterMetadata('CLOUD_COVERAGE_ASSESSMENT', 'less_than',10);
//Map.addLayer(s2SR, {bands:['B4', 'B3', 'B2'], min:0, max:8000}, 's2SR');
// adding mNDWI function
var addMNDWI = function(image) {
var mndwi = ee.Image(image).normalizedDifference(['B3', 'B11']).rename('MNDWI');
return ee.Image(image).addBands(mndwi);
};
var mndwı=s2SR
.map(addMNDWI);
Map.addLayer(mndwı.first(), { min:245, max:5000}, 'mndwı');
It is simple to view the values for any displayed image. First, click on the “Inspector” tab in the top right pane of the Earth Engine Code Editor.
Then, click wherever you want on the map. The Inspector tab will display:
The coordinates of the location you clicked.
The values of every band of every image under that point. (When there are many, as a chart.)
The details of the image (or feature), including properties.

bokeh change circle colors with select or button

I am trying to change the colour of my data points with bokeh. When I use a Hover tool this works fine. However, if I use the same callback function with a select or button tool it does not work. I guess this is beacause the change.emit() does not work in combination with a button or select?
How can I make my customJS work with a select or button tool?
callback3=CustomJS(args=dict(source2=source2,p2=p2),code=''' var source2=source2 var data3 = source2.data;
var color = data3['color'];
var i, n = color.length;
for (i = 0; i < n; ++i) {
color[i] = 'blue';
source2.change.emit();
}
''' )
For my the hoover tool I use:
plot.add_tools(HoverTool(tooltips=None, callback=callback3, renderers=[d],mode='vline'))
For the button:
button = Button(label="Foo", button_type="success")
button.js_on_click(callback3)
When I use an alert in my callback this works also for the button and the select.
I solved the problem. It was not related to the change.emit(). The problem was that I used show separately for the plot and the button.

Is it possible to set column name dynamically on a dojo grid?

Is there any way to set a dynamic name for column header, for example in a formatter function!?
I'll have a popup menu on the grid and depending on the chosen option it should change the name displayed in the column header.
Its quite easy if you use dojo.query
var grid = dijit.byId('myGridId'), NewHeader = "Foo Bar Text Content";
var columnHeaderNodes = dojo.query(
'.dojoxGridHeader table th',
grid.viewsHeaderNode)
var nthColumn = 12;
// if has child and its not a textnode - this may happen
// when there is a listener (dnd, click) attached for sorting etc.
if(columnHeaderNodes[nthColumn].firstChild && columnHeaderNodes[nthColumn].firstChild.nodeType != 3)
tgt = columnHeaderNodes[nthColumn].firstChild;
else tgt = columnHeaderNodes[nthColumn];
tgt.innerHTML = NewHeader;

Changing individual tab style in flex

I have figured out a way to change the style of tabs at run time with following logic:
var cssStyle:CSSStyleDeclaration = StyleManager.getStyleDeclaration(".MyTabs");
cssStyle.setStyle("borderColor", "red");
But here ".MyTabs" class is applicable to all the tabs between first and last tab. As per getStyleDeclaration javadoc, it only accepts "class selector" and "type selector" not the id selector.
How can I change the individual tab style at run time?
(tabNavigator.getTabAt(index) as Button).setStyle("borderColor", 0xFF0000);
This will solve the issue, you can set your own color as value param.
Another user pointed out a method that I had somehow missed, allowing you to access a Tab as a Buttom and style it from there.
var t:Button = theTabs.getTabAt(index);
Tab extends Button, so there may be some things you would need the below solution for, but for basic styling this should be enough.
#Sebastian's answer works for a TabBar, which I know you don't have, as this is the third identical question you've asked. In order to style the tabs on a TabNavigator, you need to access the internal TabBar.
//this import may not auto-complete for you
import mx.controls.tabBarClasses.Tab;
var t:Tab = theTabs.mx_internal::getTabBar().getChildAt(index);
Now you can feel free to set the styles, as shown in Sebastian's answer.
You can call setStyle on the individual Tab's, which u can get by calling TabBar.getChildAt(x) . Check the following link, which illustrates how to achieve the task you are trying to perform. You can also check out this link
private function tabBar_creationComplete():void {
var colorArr:Array = ["red", "haloOrange", "yellow", "haloGreen", "haloBlue"];
var color:String;
var tab:Tab;
var idx:uint;
var len:uint = tabBar.dataProvider.length;
for (idx = 0; idx < len; idx++) {
var i:int = idx % colorArr.length;
color = colorArr[i];
tab = Tab(tabBar.getChildAt(idx));
tab.setStyle("fillColors", [color, "white"]);
tab.setStyle("fillAlphas", [1.0, 1.0]);
tab.setStyle("backgroundColor", color);
}
}

Dojo Datagrid: How to change the style of the first row?

I am new to DoJo development so this could be basic.
I have created an EnhancedDatagrid and it shows the data fine.
The data comes from an JSON store in a different page.
I have a button which causes that one new entry is created in the datastore and then my datagrid is 'refreshed'. This works fine.
But now i want only as the last step to change the style of the first row in my datagrid.
(I need to make the newly added row more visible.)
But i simply can't figure out how to get a handle on the first row in a datagrid.
...
grid = new dojox.grid.EnhancedGrid({
id: strId,
store: store,
structure: layout,
}, document.createElement('div'));
dojo.byId(placeHolder).appendChild(grid.domNode);
grid.startup();
var row = grid.getItem(0); // ---get the first row. How ? And how to apply new style ?
...
Thank you in advance.
Solved the problem like this:
dojo.connect(grid, 'onStyleRow', this, function (row) {
var item = grid.getItem(row.index);
if (row.index == 0) {
row.customClasses = "highlightRow";
row.customStyles += 'background-color:#FFB93F;';
}
});
I use the 'Claro' theme and it prevented me to set the background color of the row-cells.
The solution was to set the customClasses to a style like this:
.highlightRow tr
{
background-color: #FF6A00 !important;
}
Found part of the solution here: http://dojo-toolkit.33424.n3.nabble.com/row-customStyles-was-overwrite-by-claro-theme-td3763079.html

Resources