简体   繁体   English

Google Sheets Scripts - 对于范围内的每个单元格,获取单元格具有特定值的行号,将行复制到范围内的第一个空白行

[英]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的另一种方法。

Sample Code:示例代码:

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);
  }
}

What it does?它能做什么?

  1. Get the sourceSheet and cell modified using e.range event object获取使用 e.range 事件 object 修改的 sourceSheet 和单元格
  2. Check the cell modified if it is in column Q (index 17) of Sheet1 having a value of "TRUE".检查修改的单元格是否位于 Sheet1 的 Q 列(索引 17)中,值为“TRUE”。 I used Range.getDisplayValue() to get the actual string value of the cell because when I test it using Range.getValue() it gave me a boolean object我使用Range.getDisplayValue()来获取单元格的实际字符串值,因为当我使用Range.getValue()对其进行测试时,它给了我一个 boolean object
  3. Get the row values from the first column up to the last column of the current row using Sheet.getLastColumn() and Sheet.getRange(row, column, numRows, numColumns)使用Sheet.getLastColumn()Sheet.getRange(row, column, numRows, numColumns)获取当前行的第一列到最后一列的行值
  4. Append the current row values of the source sheet to your destination sheet using Sheet.appendRow(rowContents) where 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 Append 使用Sheet.appendRow(rowContents)将源工作表的当前行值到目标工作表,其中rowContents是一维数组,由于Range.getValues()返回一个二维数组,我使用了 array.flat()将二维数组转换为一维数组
  5. Delete the current row using Sheet.deleteRow(rowPosition)使用Sheet.deleteRow(rowPosition)删除当前行

Output: Output:

Before (Sheet1):之前(表 1):

在此处输入图像描述

After (Sheet1):之后(表 1):

在此处输入图像描述

After (Sheet2):之后(表 2):

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Sheets 脚本 - 对于范围 1 中的每个使用过的单元格,如果单元格值存在于范围 2 中,则获取范围 2 匹配的行号 - Google Sheets script - For each used cell in range 1, if cell value exists in range 2, get range 2 match's row number 将单元格复制并粘贴到特定列谷歌表格中的第一个空行 - Copy and Paste a Cell to First Empty Row in Specific Column google sheets Google 表格脚本:获取命名范围中的第一行号 - Google Sheets Script: Get First Row Number in Named Range 根据单元格值将特定范围(不是整行)复制到另一个Google工作表 - Copy over a specific range not whole row to another google sheet based on cell value Google 电子表格:根据该行中的单元格值从范围复制行 - Google spreadsheet :Copying row from a range based on a cell value in that row Google表格根据单元格编号值添加行具有值的单元格计数 - Google Sheets Add row based on cell number value the count of cell which has value 在值复制/粘贴或插入/编辑谷歌表格中的整行时插入时间戳,仅插入空白单元格 - Insert timestamp when value copy/paste or inserts/edits whole row in google sheets, only insert to blank cell Google 应用程序脚本 - 获取包含特定值的单元格的行号 - Google apps script - Get row number of a cell containing specific value 如何定义命名范围的第一行号? (Google表格) - How to define the first row number of a Named Range? (Google Sheets) 根据条件和单元格值将行复制到 Google 表格上的另一张表格 - Copy row to another sheet on Google Sheets based on criteria and cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM