[英]Google Sheets Scripts - For each cell in range, get row number where cell has specific value, copy row to first blank row in range
I come from a VB background and am having some trouble implementing a scripts solution for Sheets, I imagine the function is fairly basic but am not yet sure how to express it in JS.我来自 VB 背景,在为 Sheets 实现脚本解决方案时遇到了一些麻烦,我想 function 相当基本,但还不确定如何在 JS 中表达它。 The goal is to loop through each cell in a range (Sheet 1, Q3:Q1000), get the row number where the cell in that range has the value "TRUE", copy/cut the entirety of each row on Sheet 1 that meets that qualification (or simply store the values and skip the copy/paste step), paste the values to the first unused row on a separate sheet (Sheet2), then delete the original row on Sheet1.
目标是遍历范围内的每个单元格(表 1,Q3:Q1000),获取该范围内单元格值为“TRUE”的行号,复制/剪切表 1 上满足的每一行的全部内容该限定条件(或简单地存储值并跳过复制/粘贴步骤),将值粘贴到单独的工作表(Sheet2)上的第一个未使用的行,然后删除 Sheet1 上的原始行。
So far, I have managed to put together this crude function which successfully finds the row number of the first cell in the given range containing the specified value, but it returns NaN if there is more than one occurrence of the value in the range.到目前为止,我已经设法将这个粗略的 function 放在一起,它成功地找到了给定范围内包含指定值的第一个单元格的行号,但如果在该范围内多次出现该值,它将返回 NaN。 Any indication as to what I am doing wrong will be helpful, thank you.
任何关于我做错了什么的迹象都会有所帮助,谢谢。
function onEditTesting(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName("Sheet1");
const range = "Q3:1000";
var whereTrue = "TRUE";
var range = sheet.getRange("Q3:Q1000");
var rangeValues = sheet.getRange(range).getValues();
var i = [];
for (var y = 0; y < rangeValues.length; y++) {
if(rangeValues[y] == whereTrue){
i.push(y);
}
}
var Row = Number(i)+Number(range.getRow());
}
function moveData() {
const ss=SpreadsheetApp.getActive();
const ssh=ss.getSheetByName('Sheet1');
const dsh=ss.getSheetByName('Sheet2');
const vs=ssh.getRange(3,1,ssh.getLastRow()-2,ssh.getLastColumn()).getValues();
let oA=[];
let d=0;
vs.forEach((r,i)=>{
if(r[16]=='TRUE') {
oA.push(r);
ssh.deleteRow(i+3-d++);
}
});
dsh.getRange(dsh.getLastRow()+1,1,oA.length,oA[0].length).setValues(oa);
}
Here is another approach using onEdit() Simple Trigger .这是使用onEdit() Simple Trigger的另一种方法。
function onEdit(e) {
var ss = e.source;
var cell = e.range;
var sourceSheet = ss.getActiveSheet();
var destinationSheet = ss.getSheetByName("Sheet2");
var col = cell.getColumn();
var row = cell.getRow();
var lastCol = sourceSheet.getLastColumn();
Logger.log("row: "+row+" maxCol: "+lastCol+" col: "+col+" val: "+cell.getDisplayValue()+" sheet: "+sourceSheet.getName());
//Check if modified cell is in column Q (index 17) in Sheet1 has a value of TRUE
if(sourceSheet.getName() == "Sheet1" && col == 17 && cell.getDisplayValue() == "TRUE"){
//Append current row to the destination sheet
var values = sourceSheet.getRange(row,1,1,lastCol).getValues();
Logger.log(values);
destinationSheet.appendRow(values.flat());
//Delete current row
sourceSheet.deleteRow(row);
}
}
rowContents
is a 1-d array and since the Range.getValues() returns a 2-d array, I used array.flat() to convert 2-d array to 1-d array rowContents
是一维数组,由于Range.getValues()返回一个二维数组,我使用了 array.flat()将二维数组转换为一维数组 Before (Sheet1):之前(表 1):
After (Sheet1):之后(表 1):
After (Sheet2):之后(表 2):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.