我在Google Spreadsheets中有一个脚本,用于删除包含4个或更多数字的alls行。我想修改它以删除在星号标志之前具有3个或更少字符(数字或其他)的所有行,*。
以下是当前形式的脚本:
function removeRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 0;
var val = sheet.getRange(2, 1, lr-1, 2).getValues();
var lr = sheet.getLastRow()
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
var str = row[0].replace(/\D/g, '')//extract the numbers
var len = str.length // determine lenght of number string
if(len >=4 ){ //if 4 or more numbers delete
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
for (var i = 0; i <= val.length-1; i++) {
var str=val[i][0].split(/[*]/)//split at *
var numStr = str[0].replace(/\D/g, '');
var len = numStr.length // determine length of string of first split
if(len <=3 ){ //if 3 or less, delete
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
}
答案 0 :(得分:1)
您可以使用https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.9/#execaction-v1-core来测试单元格内容中是否存在该字符,而不是split
字符处*
字符。如果有*
,则返回的整数将为0或更多。值为4或更多表示搜索字符前面有4个或更多字符,而值0到3表示前面有0到3个字符。
此代码段假设您的工作表数据位于名为val
的数组数组中(就像您在上面发布的代码中所做的那样)。
var colIndex = 0; // Which column in the range should be checked for an *.
for(var i = 0; i < val.length; ++i) {
var charBefore = String(val[i][colIndex]).indexOf("*");
if(charBefore < 4 && charBefore >= 0) {
/*
* There are 3 or fewer characters preceding the asterisk.
* Add code in this block to work with i, val, val[i], or val[i][colIndex]
* for this specific case.
*/
} else {
/*
* There was no asterisk in the cell, or it was preceded by 4+ characters.
* Add code to this block (or remove the else block entirely, if nothing is to
* be done with rows / cells that did not meet the specified asterisk criteria.
*/
}
}
答案 1 :(得分:0)
在您的代码段中,在分割行的值后,您没有提取数字。 试试这个: -
for (var i = 0; i <= val.length-1; i++) {
var str=val[i][0].split(/[*]/)//split at *
var numStr = str[0].replace(/\D/g, '');
var len=numStr.length // determine length of string of first split
if(len <=3 ){ //if 3 or less, delete
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}