Working with Google Apps Script .getRowHeight() on fit to data sheet - google-apps-script

I am currently trying to write some Google Apps Script script that would allow my Google sheet table to fit to data, but with some row height cap.
The easiest way that came into my mind was first fit everything to data, and then walk over every row to check whether its height is more than some value and then fix it if it is too high.
sheet.autoResizeRows(startRow, numRows)
for (var i=0;i<numRows;i++){
var curHeight = sheet.getRowHeight(startRow + i)
Logger.log(curHeight)
if (curHeight > 50){
sheet.setRowHeight(startRow + i, 50)
}
}
However, what I found out is that .getRowHeight() always returns 20.0 - the default value.
Thus, do I miss something or .getRowHeight() does not work properly with fit to data rows, and if not is there a workaround?

your code is perfect.
So by-default the row size of google spreadsheet is 20 or 21 pixel.
hence:
if you want to set a fixed provided height to all rows then use "if(curHeight != 50)"
which will set height irrespective of whether the current height is higher or lower than the new value provided.
Else, if you just want to check your script, change the value to a value lesser than the default height i.e. if(curHeight > 10)

can I ask if you ever got a solution to this?
I'm having exactly the same problem. I wrote a function to "prettify" a sheet to make all the row heights the same (based on the greatest row height required to fit the data), but it fails because, as you found, sheet.getrowHeight() always seems to return the default value of 20 pixels, rather than the actual row height on the sheet.

Related

How to have google sheets autocomplete some aspects of a function, but keep others the same

I need to be able to replicate these function on a large scale:
=INDEX(List1!A1:G21, MATCH(F2, List1!A1:A21), MATCH(E2, List1!B1:F1)+1)
but I need these aspects to stay the same:
A1:G21
A1:A21
B1:F1
and this to change according to their position in the sheet:
F2:E2
I also need this function:
=INDEX(List1!A1:G21, MATCH(F2, List1!A1:A21), 7)
and need to change this value according to is position:
F2
but need this to stay the same:
A1:G21
A1:A21
7
I had tried using Google's autocomplete, but it obviously scales all the value in accordance to its position and since I am very new to sheets, I haven't a clue on what I could do. I tried writing a js function, but my experience in js is little and I couldn't keep up.
You can't put an $ before the aspect you want fixated:
$A$1 would never move
$A1 would be always in Column 1 and change its number
A$1 would only move the column but not the row

App Script - Get row height in a table in SlidesApp

I'm iterating over a table with variable row sizes. Some rows cause the table to flow outside of the slide. To fix, I thought I'd set a maximum table size and add up all of the row sizes after setting the text and if it's too big create a new slide.
However, each row is always the same height. My assumption is that since it's not rendered in the UI yet, that all the rows remain the same size. I thought refreshing the slide would do it but it is not. I'm new to App Script (1 day).
updateRow(row, data);
slide.refreshSlide();
tableHeight += row.getMinimumHeight();
As an aside, I also attempted to get the size of the table using the
Table.getHeight()
Table.getInherentHeight()
methods but they continued to return null for a reason I didn't understand.

Google Sheets setRowHeight() auto-fitting, NOT resizing to set value

I've a Google form/sheet which collects data creating cells that are so large that navigating up and down rows becomes tricky.
The below function should resize all rows (except the header row 1) to 50.
However, when it runs, all rows Auto-fit the data, again making it again unwieldy.
var sheetResponses = 'Form responses 3';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var responsesSheet = ss.getSheetByName(sheetResponses);
var responseData = responsesSheet.getDataRange().getValues();
// Sets all rows to a height of 50
function resizeRowsTo50() {
responsesSheet.setRowHeights(2,responseData.length,50);
};
Can anyone spot what I've done wrong? Even if I manually resize all of the rows to a uniform height beforehand, the function reverts them back to fit the data.
Thanks in advance.
Edit: Link to Sheet
It appears that setWrap and setWraps override any manual or
programmed (setRowHeight/setRowHeights) row height adjustment.
The documentation for setWrap and setWraps says "Cells with wrap enabled (the default) resize to display their full content" (emphasis is mine). That is, the cell width remains unchanged, and the row height changes to enable display of the entire content.
It goes on... "Cells with wrap disabled display as much as possible in the cell without resizing or running to multiple lines". In the later case, the amount of text displayed otherwise depends on the width of the column.
By comparison, when the settings are adjusted manually (the row height set to 50 pixels and text wrapping set to wrap), the row does not revert to auto-fit.
I've re-run my initial tests and I believe that I may misinterpreted your auto-fit comments when comparing my results. My apologies.
The auto-fit outcome appears deliberate on Google's part - even though the effect is very different to the effect of the manual "Wrap" adjustment. These are relatively new commands (at the time of writing) and there are very few examples to be found online.
I've raised this as a feature issue and we'll see whether that generates any clarification and/or change.
I found a poor decision for me in this tricky way:
- set Wrap to text wrapping;
- set setRowHeight;
- cut the string to such symbols amount which is enough for setted Row Height.
(but you still can see "fit to data" when you trying to resize your row)
I mean I fit my data to my rows hight before google sheet fit hight to my data:)

Color cell RED if cell above value is lower

I need a formula/script for a Google spreadsheet that will do this:
If the current cell value is higher than the value in the cell above the make the current cell background red (if less than or equal to then leave white), something like this: =IF((C34>B34),"make background red","leave background white") just not sure if this will work or I need a more complex script.
I need this formula to work across 224 cells (28 columns and 8 rows). Conditional formatting wont work.
There will be upto 20 people viewing the document on the day, only one will be editing the data. Will a script slow down the working of the live spreadsheet as I have a few more quite complex formulas I need to calculate the data from the main sheet to a summary sheet.
I have searched here and other forums but everyone's formulas are unique!
Cheers
Conditional formatting wont work.
Probably not at the time that was written but it does now.
Assuming your array starts in B34 for its top left, clear formatting and select B35:AC41 and Format, Conditional formatting..., Format cells if... Custom formula is and:
=A35>A34
select red fill and Done.
You can use an onEdit() trigger to react to changes by reading the value from the cell above the one-just-changed, making the comparison, and coloring appropriately. You must do this in a script, you cannot control color from custom functions.
You said "If the current cell value is higher than the value in the cell above...", but your example then had =IF((C34>B34)..., which is "beside", not "above". This code uses .offset(-1,0) for "above", and ensures it won't mess with the row above row 1 - if you meant "beside", you'll want to change that.
function onEdit(event)
{
if (isNaN(event.value)) return; // If change was not a number, exit
var changedCell = event.range;
if (changedCell.getRow() == 1) return; // Nothing to do in Row 1
var cellAbove = changedCell.offset(-1, 0);
var background = 'white'; // Assume white background
// Is the changed value greater than the value in the cell above?
if ( parseFloat(event.value) > parseFloat(cellAbove.getValue()) ) {
background = 'red'; // Yes, so red background
}
changedCell.setBackground(background);
}
WRT performance, the trigger function will slow things a bit, but I suspect you'll see a greater lag due to the large number of multiple viewers, as google-docs does it thing to keep all those disparate views in sync. (I've seen sheets with < 100 cells and no formulas struggle to keep up with < 10 viewers.)

Dynamic row height for table

Using Reporting Services I need to make a table where I can set the row height dynamically depending on the length of the text in the cell, so when I export in Excel the height of the row will expand accordingly to a size that can be printed out without hiding any information It is in that row. I noticed that there is a problem with exporting in Excel, and that the propriety 'CanGrow' is not really working to expand the row height as it should.
So, there is a way I can set a height for every table row, or a method to make Excel export with a correct height every row depending on the length of the text in the cell?
Try this code. I have used this code to set the height of row according to length of text:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
float a = 54.0f * [[yourArray objectAtIndex:indexPath.row - 1] length]/25.0f;
return a;
}