I am unsuccessfully trying to reset my row heights on a regular basis to a certain value:
function clearGuestlist() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("FormsGuestlist");
var drucksheet = ss.getSheetByName("Druck");
var tabletsheet = ss.getSheetByName("Tablet");
var druckrange = drucksheet.getRange('Druck!1:200');
var tabletrange = tabletsheet.getRange('Tablet!1:200');
first.clearContents();
drucksheet.autoResizeRows(1, 200);
tabletsheet.autoResizeRows(1, 200);
drucksheet.setRowHeight(druckrange, 31);
tabletsheet.setRowHeight(tabletrange, 31);
}
The autoResizeRows unfortunately doesn't work in my case so I would like to use the setRowHeight, but it wouldn't accept my range as it expects an int.
Any idea how I can tell it to resize all my rows?
The Range class has a getRow() method that you can use to retrieve the row position as an integer.
You can call this on the druckrange and tabletrange objects to get the position and then use setRowHeights() to set the height for your selected number of rows. Note that you have to use setRowHeights() instead of setRowHeight() because you have to plug in the start row and number of rows, like in autoResizeRows(), whereas setRowHeight() just takes the position of a single row.
Your code should look something like this:
function clearGuestlist() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("FormsGuestlist");
var drucksheet = ss.getSheetByName("Druck");
var tabletsheet = ss.getSheetByName("Tablet");
var druckrange = drucksheet.getRange('Druck!1:200');
var tabletrange = tabletsheet.getRange('Tablet!1:200');
first.clearContents();
drucksheet.setRowHeights(druckrange.getRow(), 200, 31); //gets the row position of druckrange as an int, then sets height to 31 pixels for 200 rows
tabletsheet.setRowHeights(tabletrange.getRow(), 200, 31); //same as above for tabletrange
}
Related
Basically, this is what I want to do as a custom formula for a conditional format in Google Sheets:
If any cell in range + any other cell in range = the value I specify;
Return those cells (i.e. format them as I say).
What I'm doing is, I have a column of about 80 numerical (currency) values and I'm trying to figure out if any two of them sum to a given value.
Here is a sheet where i've demonstrated the solution with this (somewhat) simple formula:
=ARRAYFORMULA(QUERY(SPLIT(TRANSPOSE(SPLIT(TEXTJOIN("#",TRUE,IF(ROW(B2:B100)<TRANSPOSE(ROW(B2:B100))*(B2:B100<>""),B2:B100&"|"&TRANSPOSE(B2:B100),)),"#")),"|"),"where Col1+Col2="&D2))
Rafa Guillermo is right. It would be much easier to do it with a script, you can try this one:
function sum() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1"); //Enter the name of your sheet.
var column = 1; //Choose the number of column you want
var startRow = 2; //Choose the number of the first row with values
var lastRow = sheet.getLastRow();
var wantedSum = 50; //Your desired value.
var values = sheet.getRange(startRow, column, lastRow-1).getValues();
for (var i = 0;i<lastRow-startRow+1;i++) {
var a = values[i][0];
for (var j = 0;j<lastRow-startRow+1;j++) {
var b = values[j][0];
if (a+b == wantedSum) {
sheet.getRange(startRow+i, column).setBackground("green"); //You can edit this to the color or format you want.
sheet.getRange(startRow+j, column).setBackground("green");
return; // This stops when it finds the first pair that sums what you want, but there may be other pairs.
}
}
}
}
Remember to edit the name of your sheet, the column, the first row with values of that column and the value you are looking for.
I have a large sheet with over 4000 rows and 30 columns.
I am trying to automaticly add new rows to the sheet if there are less than x "empty" rows. Empty rows are defined by just containing formulas but the data column A is empty.
To check if a row is empty or not I check column A, because you have to enter data in column A. After adding new rows the script should copy/paste the last row before over the new rows, so they contain the formulas as well.
Here is the script (PREVIOUS VERSION):
function addRowsItems() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName('items');
var freeRows = 300; // Number of empty Rows after last Entry
var lRow = sh.getLastRow(), lCol = sh.getLastColumn(), range = sh.getRange(lRow,1,1,lCol);
var startRow = lRow-freeRows;
if(startRow < 0) {
startRow = 1; }
var values = sh.getRange("A" + startRow + ":A").getValues();
var maxIndex = values.reduce(function(maxIndex, row, index) {
return row[0] === "" ? maxIndex : index;
}, 0);
var maxIndex = maxIndex + startRow;
var space = lRow - maxIndex;
var addLines = freeRows - space;
if(space < freeRows) {
sh.insertRowsAfter(lRow, addLines);
range.copyTo(sh.getRange(lRow+1, 1, addLines, lCol), {contentsOnly:false});
}
}
This is working in a new sheet with less data.
But using it in the main sheet with over 4000 rows of data I get a time out. The script adds the new rows but it times out before it is able to copy/paste.
Why is it taking so much time? Is there an easier way to achieve that?
So this is the actual version. Cell B1 contains COUNBLANK of Range A:A.
function addRowsItems() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName('items');
var freeRows = 307; // Number of empty Rows after last Entry
var lRow = sh.getMaxRows(), lCol = sh.getMaxColumns(), range = sh.getRange(lRow,1,1,lCol);
var space = sh.getRange("B1").getValues();
var addLines = freeRows - space;
if(space < freeRows) {
sh.insertRowsAfter(lRow, addLines);
range.copyTo(sh.getRange(lRow+1, 1, addLines, lCol), {contentsOnly:false});
}
}
Try the COUNTBLANK function returns a count of empty cells in a range. Cells that contain text, numbers, errors, etc. are not counted. Formulas that return empty text are counted.
I have created a google sheet that contains a button that when clicked will create a random number in a particular column. However, I need the code to find the first empty cell in the column for the random number. The code I am currently using fills all the cells in the row verses one cell at a time when the button is clicked.
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [ {name:"Resubmit",functionName:"Resubmit"} ];
sheet.addMenu("Script", entries);
};
function Resubmit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var cell = sheet.getRange("A:A");
cell.setValue( Math.floor((Math.random()*1000000)+1) );
};
You can be more specific and give it a number of cells you want, as per docs:
getRange(row, column, numRows, numColumns)
You can append after the last row either by setting cell like this:
var cell = sheet.getRange(sheet.getLastRow() + 1, 1);
or by changing the function to this:
function Resubmit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");;
sheet.appendRow([Math.floor((Math.random()*1000000)+1)])
};
screenshot
Hi all, i need help and i am not a coder. I am trying to achieve the same thing on sheet number 2.
My datas are imported through "=Submission!$b2" from sheet 1
i need help removing rows automatically when a specific cell on column H does not contain the value "Bekreft", i tried both codes shown here with no success.
This is what i added for script:
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('DATA - Do Not Touch!!!'); // change to your own
var values = s.getDataRange().getValues();
for(var i=values.length;i>0;i-=1){
var lcVal=values[i-1][0].toLowerCase() //Change to all lower case
var index = lcVal.indexOf("vent"); //now you only have to check for contains "vent"
if (lcVal.indexOf("vent") > -1){
s.deleteRow(i)};
}}
Seeing as you state you are "not a coder", and the code you pasted will not help you if you are referencing data from another page, I would suggest using a filter function to achieve your goal. You would add the following formula to your second page:
=FILTER(Submission!B:B,ISNUMBER(SEARCH("Bekreft", Submission!H:H)))
If you are looking to have a script go through your static list and delete out values that do not contain "Bekreft" then you can use the following script.
function onEdit() {
var sheet = SpreadsheetApp.openById("Add Sheet ID").getSheetByName('Sheet1');
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 0;
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
//row for condition
if (row[7].indexOf("Bekreft")) {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
I'm trying to create a script in google sheets. What I'm trying to do is to search the value of a cell (let's say A2 sheet1), then change the value of another cell (B2 sheet2). When the first iteration is over, proceed to the next row (A3 sheet1) and again change the value of B2. Also wait 10 secs at the end of every iteration.
I don't know why it is not working. After I run the code it just says "Finished", but nothing has changed.
function test() {
var ss = SpreadsheetApp.getActive();
var gensheet = ss.getSheetByName('Generador SE');
var clientsheet = ss.getSheetByName('Clientes SE');
var clientrange = clientsheet.getRange("A1:C200");
var clientsquantity = clientrange.getNumRows()
var servicetochange = gensheet.getRange(2,2)
var i;
for (i=1;i>clientsquantity;i++){
var currentservice = clientrange.getRange(i,1).getValue;
servicetochange=gensheet.getRange().setValue(currentservice);
Utilities.sleep(10000);
}
}
I have changed some code in the loop ... now it does something (but I can't be sure it does what you expect):
function test() {
var ss = SpreadsheetApp.getActive();
var gensheet = ss.getSheetByName('Generador SE');
var clientsheet = ss.getSheetByName('Clientes SE');
var clientrange = clientsheet.getRange("A1:C200");
var clientsquantity = clientrange.getNumRows()
var servicetochange = gensheet.getRange(2,2)
var i;
for (i=1;i<clientsquantity;i++){
var currentservice = clientrange.offset(i,0,1,1).getValue();
servicetochange.offset(i,0,1,1).setValue(currentservice);
Utilities.sleep(10000);
}
}