我正在尝试在Google工作表脚本中获取一个循环,它将在A列中搜索文本字符串:“x”并清除B-X和AC-AH列中的相邻单元格。
编辑:对不起忘了说我做了什么......我的Excel VBA宏如下:
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = ActiveSheet.Range("a11:a50")
For Each rng2 In rng1
If rng2 = "x" Then rng2.Offset(0, 1).Resize(, 23).ClearContents
Next
Dim rng3 As Range
Dim rng4 As Range
Set rng3 = ActiveSheet.Range("a11:a50")
For Each rng4 In rng3
If rng4 = "x" Then rng4.Offset(0, 28).Resize(, 6).ClearContents
Next
我发现这个脚本是由Simon Staton(here)提供的,但它是删除整行而我不想删除该行,只清除其中的一些内容:
/* Delete rows */
function deleteRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var toDelete = [];
for (var row = 0; row < values.length; row++) {
for(var column = 0;column<values[row].length;column++){
if (values[row][column].indexOf("WordThatExistsInOneRow") > -1){
toDelete.push(row);
}
}
}
for(var deleteRow = toDelete.length-1; deleteRow >= 0;deleteRow--){
sheet.deleteRow(toDelete[deleteRow]+1);
}
SpreadsheetApp.flush();
};
结果:这是我最后的结果,感谢Ed Nelson在下面给出的答案......我不得不把它从第11行开始,而不是第一行:
function copyfeb() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var lr = sheet.getLastRow()
var lc = sheet.getLastColumn()
var a = sheet.getRange(11, 1, lr, lc).getValues();
for (var i = 0; i < a.length-1; i++) {
if (a[i][0].indexOf("x")!= -1){
var delete1=sheet.getRange(i+11, 1, 1, 24)
var delete2=sheet.getRange(i+11, 29, 1, 6)
delete1.clearContent()
delete2.clearContent()
}}
}
答案 0 :(得分:0)
试试这个:
function deleteRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var lr=sheet.getLastRow()
var lc=sheet.getLastColumn()
var a = sheet.getRange(2, 1, lr, lc).getValues();
for (var i = 0; i < a.length-1; i++) {
if (a[i][0].indexOf("x")!= -1){
var delete1=sheet.getRange(i+2, 2, 1, 23)
var delete2=sheet.getRange(i+2, 29, 1, 6)
delete1.clearContent()
delete2.clearContent()
}}}