How to select row in ImageJ ResultsTable? - imagej

From an ImageJ plugin, I would like to highlight a row in a ResultsTable, like when the row is clicked with the mouse. I suppose that the function ResultsTable.selectRow(Roi roi) can do this, but I do not know how to specify the row number using a Roi. How to create a Roi for this?

Here is a code snippet to select a row by index:
import java.awt.Frame;
import ij.WindowManager;
import ij.text.TextWindow;
...
int index = ...; // row index to highlight
Frame frame = WindowManager.getFrame("Results");
((TextWindow)frame).getTextPanel().setSelection(index, index);
I adapted it from the ResultsTable source code.

Related

How to subtract range from last non empty cell to first?

I am a beginner in google sheets and I couldn't get around this formula. I have range of cells and I want to subtract last non empty cell to first cell (Z-A), here is the image:
As the values are updated in columns C, D, E and so on. I want to get the last non empty cell (from right) and subtract the values by moving backward (left). Like this:
sub = 10(Column G)-0(Column F)-10(Column E)-0(Column D)-10(Column C)
Can we devise a formula which will get the last non empty cell and subtract values until the first value? Here is the link to the sample sheet Thank you
try:
=LOOKUP(1, INDEX(1/(C2:F2<>"")), C2:F2)-(SUM(C2:F2)-
LOOKUP(1, INDEX(1/(C2:F2<>"")), C2:F2))
Suggestion: Use a custom function
You may use the following script as a custom function to get the difference between the value of the last cell and the sum of the other cells:
function SUBTRACTFROMLASTNUMBER(data) { //you can rename the custom function name
var sum = 0;
var data2 = data[0].filter(x => {
return (x != "") ? x : null;
}); //filtered data
var lastNumber = data2.pop(); //last number
data2.map(x => sum += x); //sums the remaining values
return (lastNumber - sum); //returns the output
}
This custom function extracts the selected data from the sheet and then separates the value of the last cell using pop() and then filters and sums the remaining data using filter() and map() and then subtracts the sum from the value of the last cell.
Usage
You may use this function as:
=SUBTRACTFROMLASTNUMBER(<range>)
Reference:
How to Manipulate Arrays in JavaScript

Cohort table conditional formatting

I'm looking for a way to conditionally format a cohort table in Google Sheets so that the colors will change from red (low values) through yellow (medium values) to green (high values) based on the values in each row. Anyone knows if this is possible?
Also, choosing the "Color scale" option in conditional formatting menu doesn't work because it colors the table based on the values of the full table, not each row individually.
I can use that option only if I apply it to each row individually, but my dataset has thousands of entries so that doesn't work for me.
Example table: https://docs.google.com/spreadsheets/d/1gpLMdgfs10Flt-VTtsc68E3Feju2H8UQhnai-R_9b3k/copy
Thanks in advance you guys are the greatest!
I wrote a simple script in Apps Script to apply the formatting to every row. It achieves the gradient per row that you want.
Example:
function applyColorGradientConditionalFormat(min = '#FF0000', mid = '#FF9900', max = '#00FF00') {
const sheet = SpreadsheetApp.getActiveSheet();
const lastCol = sheet.getLastColumn();
const conditionalFormatRules = sheet.getConditionalFormatRules();
// Build this conditional rule for all rows in sheet
for (let i = 2; i <= sheet.getLastRow(); i++) {
let range = sheet.getRange('R' + i + 'C2:R' + i + 'C' + lastCol);
let rule = SpreadsheetApp.newConditionalFormatRule()
.setRanges([range])
.setGradientMinpoint(min)
.setGradientMidpointWithValue(mid, SpreadsheetApp.InterpolationType.PERCENT, '50')
.setGradientMaxpoint(max)
.build()
conditionalFormatRules.push(rule);
}
// Apply all conditional rules built to the sheet
sheet.setConditionalFormatRules(conditionalFormatRules);
};
// Easy improvements: Create a menu to build all conditional formats manually
// or setup triggers to do it automatically
For your sample sheet this results in the following table:
Useful documentation:
Class ConditionalFormatRuleBuilder
getConditionalFormatRules()
non scripted:
=(B2:M2=MAX($B2:$M2))*($B2:$M2<>"")
=(B2:M2=MIN($B2:$M2))*($B2:$M2<>"")

Filter Folium MarkerCluster by Column from Dataframe

I want to map a MarkerCluster with a filter by a column (QuadClass). I followed this answer: Filter Folium Map based on marker color
And it works for single markers, but when I try to use MarkerCluster nothing gets added to the map. is there anyway to achieve this?
Qclass = {1: "Verbal Cooperation",
2: "Material Cooperation",
3: "Verbal Conflict",
4: "Material Conflict"}
m = folium.Map(location=[33.8564, 66.0867],
tiles='Stamen Toner',
zoom_start=6)
marker_cluster = MarkerCluster().add_to(m)
features = {}
for row in pd.unique(df1["QuadClass"]):
features[row] = folium.FeatureGroup(name=row)
for index, row in df1.iterrows():
marker = folium.Marker([row['ActionGeo_Lat'], row['ActionGeo_Long']],
popup='QuadClass: '+Qclass[row['QuadClass']]).add_to(marker_cluster)
marker.add_to(features[row['QuadClass']])
for row in pd.unique(df1["QuadClass"]):
features[row].add_to(m)
folium.LayerControl().add_to(m)
m
Found out that folium has this plugin which solves the issue with the MarkerCluster: https://github.com/python-visualization/folium/blob/master/folium/plugins/feature_group_sub_group.py

Change cell value based on its background color in google sheets

Trying to ditch Excel for Google sheets.
I have this empty table with some colored cells that I need to fill with symbols. Presently I use this VBA script to do the job:
Sub mark()
Dim r As Range
Dim rCell As Range
Set r = Selection.Cells
For Each rCell In r
If rCell.Interior.ColorIndex = 10 Then rCell.Value = "×"
If rCell.Interior.ColorIndex = 3 Then rCell.Value = "×"
If rCell.Interior.ColorIndex = 45 Then rCell.Value = "×"
If rCell.Interior.ColorIndex = 1 Then rCell.Value = "×"
If rCell.Interior.ColorIndex = 15 Then rCell.Value = "×"
Next
End Sub
Is there a way to accomplish same thing using google sheets?
Solution
In order to achieve this you will have to use Google Apps Script. You can attach an Apps Script project to your Google Spreadsheet by navigating Tools > Script Editor.
You should find a template function called myFunction, a perfect starting point for your script.
Here you can start translating your VBA script to Apps Script which is very similar to Javascript.
First you should define some constants for your script:
// An array containing the color codes you want to check
const colors = ['#00ff00']; // Watch out, it's case sensitive
// A reference to the attached Spreadsheet
const ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SheetName'); // Selecting the Worksheet we want to work with by name
// Here we retrieve the color codes of the backgrounds of the range we want to check
const range = ss.getDataRange().getBackgrounds(); // Here I select all the cells with data in them
Let's now loop through our range rows and columns to apply the logic:
The .getBackgrounds() method returns a multidimensional array in the form array[row][column] -> "background-color-code".
for (let i = 0; i<range.length; i++) {
let row = range[i];
// Let's loop through the row now
for (let j = 0; j< row.length; j++) {
let color = row[j];
// If the background color code is among the ones we are checking we set the cell value to "x"
if(colors.includes(color)) {
// Javascript index notation is 0 based, Spreadsheet one though, starts from 1
ss.getRange(i+1, j+1).setValue("x"); // Let's add 1 to our indexes to reference the correct cell with the .getRange(row, column) function
}
}
}
Reference
Please take a look at the documentation for further reading and method specifications
Google Apps Script Spreadsheet Service
Range Class
.getBackgrounds()
.getRange(row,column)

Google Sheets: Conditional formatting based on another cell's style

I am trying to create a select list in Google Sheet based on cells in another sheet. Those cells contains all the values that my list should display.
It works well but I also want to retrieve the style of those cells along with the values. So in my main sheet, depending on the selected value the style is copied from the "source" cells.
I know I can setup a conditional formatting so if the value is X or Y or Z I can apply a style but since my "source" cells are going to be updated, I'll have to also update those conditions which is a slow process.
I am wondering if there is a way to just dynamically copy the style of another cell.
Here is an example of my source cells:
Using Apps Script, you could create an onEdit trigger to do the following:
Track changes to the cells that contain data validations based on values in a range.
If the edited cell contains this data validation, look for the value in the source range that corresponds to the selected value.
Copy and paste the format of the cell in source range to the selected cell.
To do that, just create a bound script by selecting Tools > Script editor, copy the following code and save the project.
Code sample (check comments):
const onEdit = e => {
// Get information on edited cell (column, row, value):
const range = e.range;
const column = range.getColumn();
const row = range.getRow();
const value = range.getValue();
// Check that edited cell contains a validation rule and that its criteria type is "VALUE_IN_RANGE":
const validation = range.getDataValidation();
if (validation && validation.getCriteriaType() == "VALUE_IN_RANGE") {
const sourceRange = validation.getCriteriaValues()[0]; // Get range validation is based on
// In source range, get index of the cell that corresponds to selected value in edited cell:
const values = sourceRange.getValues();
let i, j;
for (i = 0; i < values.length; i++) {
j = values[i].indexOf(value);
if (j != -1) break;
}
const rangeToCopy = sourceRange.getSheet().getRange(sourceRange.getRow() + i, sourceRange.getColumn() + j); // Get cell in source range to copy format
rangeToCopy.copyFormatToRange(range.getSheet(), column, column, row, row); // Copy format to edited cell
}
}
Reference:
onEdit trigger
Class DataValidation
copyFormatToRange(sheet, column, columnEnd, row, rowEnd)

Resources