Reputation: 39
I am attempting to check the values in a specific Column(B). Those values are based on a formula in the cells in Column B. What I am wanting to do is: if the length of the resulting string is greater than 15, to copy and paste the Displayed Value in the same cell (overwriting the formula). The formula that is there is a simple if(x>y, {do this},""{Leave cell blank}). If possible, I'd also like to expand it for 5 consecutive columns. The "IF" formula will be in each column.
Here is the dilemma another way. In column 'B' I have the =if(X>Y, Z{do this},""{Leave cell blank})in each row. If the formula result is true, copy the displayed value and overwrite the formula, if it is false, leave the formula there.
I have found this as a starting block, but it overwrites the formula regardless of the True/false outcome.
function myFunction() {
var s = SpreadsheetApp.getActiveSpreadsheet();
var ss = s.getSheetByName('Copy');
var range = ss.getRange('b9:b12'); //This is just a test range, the real range will be the entire column.
var cells = range.getValues();
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
for (var i = 0; i < numRows; i++) {
for (var j = 0; j < numCols; j++) {
if(cells.length > 15){
cells[i][j] ;
}
}
}
range.setValues(cells);
}
Upvotes: 0
Views: 191
Reputation: 64040
function usingstringlength() {
var s = SpreadsheetApp.getActive();
var ss = s.getSheetByName('Copy');
var range = ss.getRange('b9:b12');
var vs = range.getValues();
for(var i=0;i<vs.length;i++) {
for (var j = 0; j < vs[i].length;j++) {
if(vs[i][j].toString().length > 15){
vs[i][j]+="I am greater than 15";
}
}
}
range.setValues(vs);
}
function usingstringlength() {
var s = SpreadsheetApp.getActive();
var ss = s.getActiveSheet();
var range = ss.getDataRange();
var vs = range.getValues();
for(var i=0;i<vs.length;i++) {
for (var j = 0; j < vs[i].length;j++) {
if(vs[i][j].toString().length > 15){
ss.getRange(i+1,j+1).setValue("Whatever I'm not sure")
}
}
}
range.setValues(vs);
}
Data Before Running:
COL1,COL2,COL3,COL4,COL5,COL6,COL7,COL8
16,11,17,11,12,15,4,17
14,17,14,14,4,11,13,11
9,9,10,11,14,10,5,9
10,3,11,17,10,8,6,8
5,12,13,18,7,9,2,15
2,5,10,9,18,18,12,12
15,1,11,11,7,1,4,4
6,1,0,7,2,17,11,11
18,9,9,6,15,9,0,10
15,2,1,14,16,4,19,15
14,5,11,14,2,17,10,1
19,4,19,5,5,1,15,7
9,5,15,12,8,3,9,11
11,12,2,0,4,13,8,2
13,17,5,18,13,16,19,5
15,18,16,0,8,19,18,4
16,19,6,0,4,3,9,11
12,6,6,12,18,5,1,2
2,19,10,4,13,14,6,1
6,10,15,8,12,8,3,5
Data After Running:
COL1I am greater than 1,COL2I am greater than 1,COL3I am greater than 1,COL4I am greater than 1,COL5I am greater than 1,COL6I am greater than 1,COL7I am greater than 1,COL8I am greater than 1
16I am greater than 1,11I am greater than 1,17I am greater than 1,11I am greater than 1,12I am greater than 1,15I am greater than 1,4,17I am greater than 1
14I am greater than 1,17I am greater than 1,14I am greater than 1,14I am greater than 1,4,11I am greater than 1,13I am greater than 1,11I am greater than 1
9,9,10I am greater than 1,11I am greater than 1,14I am greater than 1,10I am greater than 1,5,9
10I am greater than 1,3,11I am greater than 1,17I am greater than 1,10I am greater than 1,8,6,8
5,12I am greater than 1,13I am greater than 1,18I am greater than 1,7,9,2,15I am greater than 1
2,5,10I am greater than 1,9,18I am greater than 1,18I am greater than 1,12I am greater than 1,12I am greater than 1
15I am greater than 1,1,11I am greater than 1,11I am greater than 1,7,1,4,4
6,1,0,7,2,17I am greater than 1,11I am greater than 1,11I am greater than 1
18I am greater than 1,9,9,6,15I am greater than 1,9,0,10I am greater than 1
15I am greater than 1,2,1,14I am greater than 1,16I am greater than 1,4,19I am greater than 1,15I am greater than 1
14I am greater than 1,5,11I am greater than 1,14I am greater than 1,2,17I am greater than 1,10I am greater than 1,1
19I am greater than 1,4,19I am greater than 1,5,5,1,15I am greater than 1,7
9,5,15I am greater than 1,12I am greater than 1,8,3,9,11I am greater than 1
11I am greater than 1,12I am greater than 1,2,0,4,13I am greater than 1,8,2
13I am greater than 1,17I am greater than 1,5,18I am greater than 1,13I am greater than 1,16I am greater than 1,19I am greater than 1,5
15I am greater than 1,18I am greater than 1,16I am greater than 1,0,8,19I am greater than 1,18I am greater than 1,4
16I am greater than 1,19I am greater than 1,6,0,4,3,9,11I am greater than 1
12I am greater than 1,6,6,12I am greater than 1,18I am greater than 1,5,1,2
2,19I am greater than 1,10I am greater than 1,4,13I am greater than 1,14I am greater than 1,6,1
6,10I am greater than 1,15I am greater than 1,8,12I am greater than 1,8,3,5
Upvotes: 1