我试图将这个vba代码复制到Google脚本中:
With ws1
colF = .Cells(.Rows.Count, "F").End(xlUp).Row
colG = .Cells(.Rows.Count, "G").End(xlUp).Row
For i = 4 To LastRow Step 1
colF = .Cells(i, "F").Value
colG = .Cells(i, "G").Value
If colF + colG > 115 Then
'copy rows etc
End if
Next
End With
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('ws1');
var lastRow = sheet.getLastRow();
var colF= sheet.getRange(6,4,lastRow)
var colG= sheet.getRange(7,4,lastRow)
for (var i=0; i <=lastRow; i++) {
if (colF ++ colG > 115) {
//copy rows etc
}
}
你能帮我找到正确的方法吗?谢谢
编辑
这是错误
"if (colF ++ colG > 115) {“错误”。
我和if (colF +++ colG > 115) {解决了问题。
我的目标是为每一行和两个值,它们位于同一行,但位于两个不同的列中。
如果结果符合">115“条件,则复制行。
我找不到正确的语法,在列中找不到值
发布于 2021-08-18 13:46:53
您的变量colF和colG是范围,而不是特定的值或行号,因此不能添加它们或将它们与数字进行比较。
为了加快行的迭代速度,获取整个数据集,获取值,然后迭代生成的2D数组。
另外,由于您只是在迭代数据集,所以数据的第一行将是索引0。在处理数据或粘贴数据时,请记住这一点,您需要将dataSet索引转换为电子表格行。
function columns(){
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('ws1');
var startRow = 4;
var lastRow = sheet.getLastRow();
var numRows = lastRow - startRow;
var lastCol = sheet.getLastColumn();//the number of columns you want to copy
//this gets a 2D array of the values from your starting row to the last row and from col 1 to the last col.
var dataSetValues = sheet.getRange(startRow, 1, numRows, lastCol).getValues();
for (var i = 0; i < lastRow; i++){
//get the value of row i, col F
//arrays are index based, to get a column from a row, subtract 1 from the column number
//in this case Col F is Col 6, so it's index is 5.
let fVal = dataSetValues[i][5];
//get the value of row i, col G
let gVal = dataSetValues[i][6];
//add the two together
// the + before each var forces app script to treat as a number, it is notoriously bad for randomly treating values as strings
let sum = +fVal + +gVal;
if (sum > 115) {
//get row i values, this is a 1D array of the column values
let row = dataSetValues[i];
//do something with row
}
}
} https://stackoverflow.com/questions/68833070
复制相似问题