我正在尝试检查特定列(B)中的值。这些值基于B列单元格中的公式。我想要做的是:如果结果字符串的长度大于15,则将Displayed Value复制并粘贴到同一单元格中(覆盖公式) 。存在的公式是一个简单的if(x> y,{do this},“” {Leave cell blank})。如果可能的话,我也想将其扩展为连续5列。 “ IF”公式将出现在每一列中。
这是另一种困境。在“ B”列中,每行中都有= if(X> Y,Z {do this},“” {Leave cell blank})。如果公式结果为true,则复制显示的值并覆盖公式;如果结果为false,则将公式保留在那里。
我发现这是一个开始,但是无论真假结果如何,它都会覆盖公式。
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);
}
答案 0 :(得分:1)
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);
}
运行前的数据:
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
运行后的数据:
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