[英]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.
这不是必需的,但我无法删除那部分代码并维护功能。
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()
函数。
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.