How to skip hidden rows while iterating through Google Spreadsheet w/ Google Apps Script - google-apps

I have a Google Spreadsheet with many hidden rows in it, and I want to skip them when iterating through a list of rows in the spreadsheet.
It's mainly an efficiency issue, as I'm dealing with over half of my rows being hidden and in no need of being checked.
Any help would be appreciated.

New API as of 2018 that's useful for this problem: isRowHiddenByUser. See also isRowFilteredByUser.

There's no direct way of doing this in Apps Script, but there is a feature request open to provide a way get the show/hide status of a row, if you want to star it.

A workaround using SUBTOTAL. Create 2 columns A and B. A must always have a value and B has a set of formulas. These 2 columns look like this:
A | B
---------------------------
1 | =NOT(SUBTOTAL(103, A1))
1 | =NOT(SUBTOTAL(103, A2))
1 | =NOT(SUBTOTAL(103, A3))
SUBTOTAL returns a subtotal using a specified aggregation function. The first argument 103 defines the type of function used for aggregation. The second argument is the range to apply the function to.
3 means COUNTA and counts the number of values in the range
+100 means ignore hidden cells in the range.
The result of SUBTOTAL with a range of 1 cell will be 0 when the cell is hidden and 1 when the cell is shown. NOT inverts it.
Now you can read the column B with your script to know if a row is hidden.
Here's the transposed question and answer: https://stackoverflow.com/a/27846180/1385429

The issue tracker holds that request since Aug 3, 2010 with a Medium priority and just a "Triaged" status. More than 3 years and no signs of solution from the GAS team.
My workaround was to use a special leading character that would indicate the visibility state of the row/column, it is a leading backtick (`) in the cells of top header row/column.
In case if merged cells are used in column headers, then an empty top row should be dedicated just for that functionality until the google engineers will improve the API.
Same applies if there are formulas in the 1st row/column cell(s).
These dedicated rows/columns itself can be hidden.
After starting to use this functionality each show/hide column/row command should be performed from a customized menu, otherwise there'll be errors when iterating through the range programmatically, because of the missing/excessive backtick.
e.g. to hide rows of selected cells the following function is invoked
function hideSelectedRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = SpreadsheetApp.getActiveRange();
// hide rows and add a ` backtick to the header cell
for (var row = range.getRow(); row <= range.getLastRow(); row++)
{
// add backtick only if it isn't there (that may happen when manually unhiding the rows)
var cellHeader = sheet.getRange(row, 1)
var cellHeaderValue = cellHeader.getValue()
if ( !cellHeaderValue.match(/^`/) ) {
cellHeader.setValue('`' + cellHeaderValue)
}
// hide rows of selected range
sheet.hideRows( row );
}
}
and the menu
SpreadsheetApp.getActiveSpreadsheet()
.addMenu("Show/Hide", [
{ name : "Hide Selected Rows", functionName : "hideSelectedRows" },
{ name : "Hide Selected Columns", functionName : "hideSelectedColumns" },
null,
{ name : "Hide Rows", functionName : "hideRows" },
{ name : "Hide Columns", functionName : "hideColumns" },
null,
{ name : "Show Rows", functionName : "showRows" },
{ name : "Show Columns", functionName : "showColumns" },
null,
{ name : "Show All Rows", functionName : "unHideAllRows" },
{ name : "Show All Columns", functionName : "unHideAllColumns" }
])
Once google engineers find the time to improve the onChange event, it will be possible to put those backticks automatically. Currently the changeType is limited to EDIT, INSERT_ROW, INSERT_COLUMN, REMOVE_ROW, REMOVE_COLUMN, INSERT_GRID, REMOVE_GRID, OTHER without any details on which Row/Column was inserted/removed. Looks like the team behind GAS is scanty. I wish they could hire more programmers (khm khm)

As for workaround, it is possible by using SUBTOTAL function which can returns a subtotal for a vertical range of cells.
Syntax is:
SUBTOTAL(function_code, range1, [range2, ...])
where hidden values can be skipped for any of these codes by prepending 10 (to the single-digit codes).
For example 102 for COUNT while skipping hidden cells, and 110 for VAR while doing so.
Related: Google Spreadsheets sum only shown rows at Webapps SE

Related

Sum a field on UITable based on the value on dropdown

I am creating an app which will perform some simple mathematical functions. One of these functions is to sum the field on my table. However, it should sum the field based on what is selected on the drop down menu and display the sum value on an edit field component. For instance, using the attached image, when I click control total, I would like to display the sum of the numbers in the fiscal period column and display it on the edit field on the right called sum.
function ControlTotalButtonPushed(app, event)
ct = app.FieldsDropDown.Value;
total = sum(app.UITable.Data.ct);
app.EditFieldTotal.Value = total;
if total == 0
app.BalanceUnbalanceLabel.Text = 'Balance';
else
app.BalanceUnbalanceLabel.Text = 'Unbalance';
end
end
I get an error:
Unrecognized variable name 'ct'
I thought app.UITable.Data.ct would refer to the column name to sum?
Please refer to the attached picture.

How to display total sum values in summary band of groups partial sums

I have a report like the image below:
Note that the sections separated by a blank space are grouped by the month and are iterated over a group band. I want to put partial in the summary band by the type of the register, like in the example figure get the sum for Register type A in January = 10, February = 5, March = 1 so as the total = 10 + 5 + 1 = 16. So the summary will look like:
How can I achieve that kind of conditional sum in jasper? Thanks in advance.
After trying a little bit with iReport I found a solution: to get the partial sum by type you have to add a variable that has the calculation type as "sum" and the reset type set as "Report". Once you set the values you have to create a variable Expression that has value only when the cell value of type is the desired value, so for the example in the question, in the column Value of the summary band in cell with the value "16" you have the expression :
$F{type}.equals("A") ? $F{value} : 0
and so on for the other types in the summary.

MS Word table formula for value of cell above [duplicate]

This question already has an answer here:
Formula code in Word 2015 [mac]
(1 answer)
Closed 6 years ago.
Is there a way in MS Word to get in table the value of the cell above for calculation?
I want to calculate the percentage of the sum.
I can build the sum with the formular =SUM(ABOVE), but I couldn't find a link to the value of the cell above. XXX in the table should be replace by a formula, something like that:
value_of_cell_above (74.970) * 0,2 => 14.994
Word's cell addressing is quite crude and doesn't have a facility for this.
In this case, your best bet is probably to use a nested field to assign the result of your =SUM(ABOVE) to a bookmark, then reference the bookmark.
e.g. in row 3, column 2, put
{ SET theSum { =SUM(ABOVE) } }{ theSum }
in row 4, column 2, put
{ ={ theSum }*0.2 }
All the {} have to be the special field code brace pairs that you can insert using Ctrl + F9 on Windows Word and typically Cmd + F9 or fn + Cmd + F9 on Mac Word.
There is a case for wrapping up sequences of fields inside a { QUOTE } field to increase the chance that if anyone deletes anything in the cell, they delete the whole calculation and not just part of it. It's sometimes easier to spot problems in that case e.g.
{ QUOTE { SET theSum { =SUM(ABOVE) } }{ theSum } }
Most of the spaces in these field codes can be removed if you prefer a minimalist approach.

Power Query - remove characters from number values

I have a table field where the data contains our memberID numbers followed by character or character + number strings
For example:
My Data
1234567Z1
2345T10
222222T10Z1
111
111A
Should Become
123456
12345
222222
111
111
I want to get just the member number (as shown in Should Become above). I.E. all the digits that are LEFT of the first character.
As the length of the member number can be different for each person (the first 1 to 7 digit) and the letters used can be different (a to z, 0 to 8 characters long), I don't think I can SPLIT the field.
Right now, in Power Query, I do 27 search and replace commands to clean this data (e.g. find T10 replace with nothing, find T20 replace with nothing, etc)
Can anyone suggest a better way to achieve this?
I did successfully create a formula for this in Excel...but I am now trying to do this in Power Query and I don't know how to convert the formula - nor am I sure this is the most efficient solution.
=iferror(value(left([MEMBERID],7)),
iferror(value(left([MEMBERID],6)),
iferror(value(left([MEMBERID],5)),
iferror(value(left([MEMBERID],4)),
iferror(value(left([MEMBERID],3)),0)
)
)
)
)
Thanks
There are likely several ways to do this. Here's one way:
Create a query Letters:
let
Source = { "a" .. "z" } & { "A" .. "Z" }
in
Source
Create a query GetFirstLetterIndex:
let
Source = (text) => let
// For each letter find out where it shows up in the text. If it doesn't show up, we will have a -1 in the list. Make that positive so that we return the index of the first letter which shows up.
firstLetterIndex = List.Transform(Letters, each let pos = Text.PositionOf(text, _), correctedPos = if pos < 0 then Text.Length(text) else pos in correctedPos),
minimumIndex = List.Min(firstLetterIndex)
in minimumIndex
in
Source
In the table containing your data, add a custom column with this formula:
Text.Range([ColumnWithData], 0, GetFirstLetterIndex([ColumnWithData]))
That formula will take everything from your data text until the first letter.

uitable 'logical' selection checkboxes

I have a doubt regarding uitable of guide Matlab.
Currently what I would like to see is how can I print a string from a uitable. This string should be like this:
(['{''ElementA'', ''true'', ''ElementB'', ''false'', ''ElementN'', ''true/false'', ...
So in the uitable there is 3 columns:
Column 1: It shows de numbers of rows (total number of Elements)
Column 2: Titled "Elements" and it shows a list of all Element names. --> Format: Let Matlab choose
Column 3: Titled "Selection" where it appears checkboxes. ---> Format: logical
So depending on the state of each check box (true or false) I would like to print an string like:
ElementA, true, ElementB, false, etc...
Any ideas ?
Thanks in advance