简体   繁体   English

谷歌表格脚本将完成的行移动到新工作表

[英]Google Sheets Script to move completed rows to new sheet

I have a script which moves data from one sheet to another when data is added to column "I".我有一个脚本,当数据添加到“I”列时,它可以将数据从一张纸移动到另一张纸。

I'd like to adapt the script to perform the action of moving all rows (values) from the 'unpaid' sheet, where column I is not empty, to the paid sheet via a button press rather than 'on edit'.我想调整脚本以执行将所有行(值)从“未付费”工作表(其中 I 列不为空)移动到付费工作表的操作,通过按下按钮而不是“编辑时”。 This will allow moving of data from multiple rows at once, rather than with the current script which only moves one row at a time.这将允许一次从多行移动数据,而不是使用一次只移动一行的当前脚本。

Please can someone assist?请问有人可以帮忙吗?

function onEdit(event) {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();


  if(s.getName() == "Unpaid" && r.getColumn() == 9 && r.getValue() != "") {
    var row = r.getRow();
    var numColumns = 11;
    var targetSheet = ss.getSheetByName("Paid");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).copyTo((target),{contentsOnly:true});
    s.deleteRow(row);
  } 
  else if(s.getName() == "Paid" && r.getColumn() == 14 && r.getValue() == false) {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Unpaid");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
  }
}

Side note, the "else if" part of the script is designed to move the code back to the original sheet.旁注,脚本的“else if”部分旨在将代码移回原始工作表。 This isn't needed but I wasn't able to delete that part of the code and maintain functionality.这不是必需的,但我无法删除那部分代码并维护功能。

Explanation:解释:

  • The first step is to find the rows in the Unpaid sheet for which column I is not empty.第一步是在Unpaid表中找到I不为空的行。 To achieve that, you can apply a ternary operator to every element of column I .为此,您可以对I列的每个元素应用三元运算符。 In more detail, you can use the map() function to check whether the element is empty or not and then apply a filter() to select only the rows with a non empty value in column I :更详细地说,您可以使用map()函数检查元素是否为空,然后应用filter()仅选择列I 中具有非空值的行:

     const movRows = iVals.map((c,i)=>c!=''?i:null).filter(f=>f!=null);
  • movRows contains the indexes for which column I is not empty. movRows包含列I不为空的索引。 To get the actual row, you need to add 2 to the elements of movRows since we started from the second row I2:I .要获得实际行,您需要将2添加到movRows的元素,因为我们从第二行I2:I

  • The next step is to iterate over the movRows array and for every element, delete the row in the Unpaid sheet and move it to Paid sheet.下一步是遍历movRows数组,对于每个元素,删除Unpaid表中的行并将其移动到Paid表中。 We are not moving it directly, but instead pushing the data to the movData array and then use this array to transfer this data for efficiency purposes.我们不是直接移动它,而是将数据推送到movData数组,然后使用这个数组来传输这些数据以提高效率。

  • The iteration needs to happen backwards otherwise every time you delete a row there will be a mismatch between the actual data and the structure of the sheet.迭代需要向后进行,否则每次删除一行时,实际数据和工作表结构之间都会出现不匹配

  • To create a button in the UI , you can use an onOpen() trigger.要在UI 中创建按钮,您可以使用onOpen()触发器。 The only thing you need to do, is to save the following script to the script editor and then a custom menu button will appear in the UI (spreadsheet).您唯一需要做的就是以下脚本保存到脚本编辑器,然后自定义菜单按钮将出现在 UI(电子表格)中。 You can click that button to execute the transfer() function.您可以单击该按钮来执行transfer()函数。


Solution:解决方案:

function transfer() {

const ss = SpreadsheetApp.getActiveSpreadsheet();
const pSh = ss.getSheetByName('Paid');
const upSh = ss.getSheetByName('Unpaid');
const numColumns = 11;
const iVals = upSh.getRange('I2:I'+upSh.getLastRow()).getValues().flat();  
const movData = [];
const movRows =  iVals.map((c,i)=>c!=''?i:null).filter(f=>f!=null);
movRows.reverse().forEach(row=>{ 
    movData.push(upSh.getRange(row+2,1,1,numColumns).getValues()[0]);
    upSh.deleteRow(row+2);                                    
  });   
pSh.getRange(pSh.getLastRow()+1,1,movData.length,movData[0].length).setValues(movData);   
}

and the custom menu button is created by using an onOpen trigger:自定义菜单按钮是通过使用onOpen触发器创建的:

function onOpen() {
  SpreadsheetApp.getUi()
  .createMenu('Macros')
  .addItem('Transfer Rows', 'transfer')
  .addToUi();
}

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

相关问题 Google表格将行移动到新表格的脚本 - Script for Google Sheets to Move Row to New Sheet 我想为谷歌工作表创建一个脚本,它将制作一张工作表的多个副本并将新工作表放在活动工作表的左侧 - I want to create a script for google sheets that will make multiple copies of a sheet and place the new sheets to the left of the active sheet 如何使用谷歌脚本检查两个谷歌工作表,并将重复项移动到新工作表? - How to use google scripts to check two google sheets, and move the duplicates to a new sheet? Google表格脚本,用于将一个表格上的一些单元格值传递到新的表格行 - Google Sheets Script for passing a few cell values on one sheet to new sheet row 谷歌工作表脚本自动添加带有复制公式的新行 - Google sheet script auto add new rows with copied formulas 使用 Google Apps 脚本,如何替换 Google 表格模板中的文本以制作新表格? - Using Google Apps Script, how can I replace text in a Google Sheets template to make a new Sheet? Google Sheets 脚本按工作表名称过滤 - Google Sheets script filter by sheet name 谷歌表提交按钮到一个新的表 - google sheets submit button to a new sheet 使用 Google 脚本更新 Google 表格中的行 - Updating rows in Google Sheets using Google script 谷歌脚本隐藏谷歌表格中的行 - Google Script to hide rows in Google Sheets
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM