[英]Script for Google Sheets to Move Row to New Sheet
I am having some issues with a shop schedule I am trying to set up. 我要设置的购物时间表存在一些问题。 I would like to have en entire row be moved the corresponding new tab when certain data is entered into column "J".
当某些数据输入到“ J”列中时,我希望将整行移动到相应的新标签页。 I have included an editable link for a sample sheet I created that also helps explain what I am trying to do.
我为我创建的样本表添加了一个可编辑的链接,该链接还有助于说明我要执行的操作。
If possible, it would also be helpful to be able to make the backgrounds change to the colors on the sample sheet as well. 如果可能的话,将背景也更改为样本表上的颜色也是有帮助的。
Thank you! 谢谢!
Editable Link: https://docs.google.com/spreadsheets/d/1D0iptPchrrLuq75ysvXwisBa_Xr0fuNabAdTb3sm-yA/edit?usp=sharing 可编辑的链接: https : //docs.google.com/spreadsheets/d/1D0iptPchrrLuq75ysvXwisBa_Xr0fuNabAdTb3sm-yA/edit? usp =sharing
This is a crude code that will work, however if you change the value to the same that it is on (so if it's in complete, and you edit it to change to 20) it will just delete the row. 这是一个粗略的代码,可以工作,但是,如果将值更改为与之相同的值(因此,如果值已完成,然后将其编辑为20),它将删除该行。 It would be easy to fix, but I just don't have the time right now :)
修复起来很容易,但是我现在没有时间:)
EDIT: just remembered to note, that it will use the last row, so for moving stuff back to appointments you should move the legend to the top of the sheet :) Or you should just get rid of the move back to appointments bit altogether. 编辑:刚刚记得要注意,它将使用最后一行,因此,为了将内容移回约会,您应该将图例移到工作表的顶部:)否则,您应该完全摆脱移回约会的位置。 You can also play around with how the variables are worked with, I just don't like grabbing the sheets again so I send a lot of them to the move function.
您还可以尝试使用变量的处理方式,我只是不喜欢再次获取工作表,因此我将其中的许多函数发送给move函数。
function onEdit(e) {
var spreadSheet = SpreadsheetApp.getActiveSpreadsheet(); //get active spreadsheet
var srcSheet = spreadSheet.getActiveSheet(); //get active sheet
var editCol = e.range.getColumn(); //check the edited column
if (editCol!==10) {return} //if the edited column is not J then we don't need to do anything else
var srcLastCol = srcSheet.getLastColumn(); //check how large is our data range
var srcRow = srcSheet.getRange(e.range.getRow(), 1, 1, srcLastCol); //get the source row range
var srcVals = srcRow.getValues(); //get the values from the source fow
var destSheet, destRow, destRange; //destination variables
Logger.log(e.value)
if (e.value>=1 && e.value<=14) { //for 1-14 we use we assign 1 sheet, the same function for everything
moveRow('In Shop', srcRow, srcVals, srcLastCol, spreadSheet);
}
else { //for everything else we use a switch
switch(e.value) {
case '15':
moveRow('Arrive / In Lot', srcRow, srcVals, srcLastCol, spreadSheet);
break;
case '17':
moveRow('Waiting on Parts, Truck, Etc.', srcRow, srcVals, srcLastCol, spreadSheet);
break;
case '20':
moveRow('Completed', srcRow, srcVals, srcLastCol, spreadSheet);
break;
default :
moveRow('Appointments', srcRow, srcVals, srcLastCol, spreadSheet);
}
}
}
function moveRow(destName, srcRow, srcVals, srcLastCol, spreadSheet) {
var destRow
var destSheet = spreadSheet.getSheetByName(destName)
destRow = destSheet.getLastRow() + 1;
destSheet.getRange(destRow, 1, 1, srcLastCol).setValues(srcVals)
srcRow.clear()
}
EDIT2 Answering comments EDIT2回答评论
function moveRow(destName, srcRow, srcVals, srcLastCol) {
var spreadSheet = SpreadsheetApp.getActiveSpreadsheet()
var destRow
var destSheet = spreadSheet.getSheetByName(destName)
destRow = destSheet.getLastRow() + 1;
destSheet.getRange(destRow, 1, 1, srcLastCol).setValues(srcVals)
srcRow.clear()
}
However, that would be bad practice for any larger script that has a long runtime. 但是,对于任何运行时间较长的较大脚本来说,这都是不好的做法。 You would then waste time accessing the SpreadsheetApp every time.
然后,您将浪费时间每次访问SpreadsheetApp。
As for the colours, it's a fairly simple adjustment in the moveRow(): 至于颜色,这是对moveRow()的相当简单的调整:
function moveRow(destName, srcRow, srcVals, srcLastCol, spreadSheet) {
var destRow
var destSheet = spreadSheet.getSheetByName(destName);
destRow = destSheet.getLastRow() + 1;
var destRange = destSheet.getRange(destRow, 1, 1, srcLastCol);
destRange.setValues(srcVals);
destRange.setBackground(destSheet.getTabColor())
srcRow.clear()
}
As for sorting, this is where you need to have destSheet = spreadSheet.getSheetByName(destName);
至于排序,这是您需要安装
destSheet = spreadSheet.getSheetByName(destName);
before calling the moveRow(). 在调用moveRow()之前。 So bellow, let's assume that you retrieve the destSheet somewhere before the
if
or in the if
itself. 因此,让我们假设您在
if
或if
本身之前的某个位置检索了destSheet。 This sample will sort it by column A in ascending order (so adjust the 1 to what you need, let's say you want J, then it would be 10) 该示例将按升序对A列进行排序(因此将1调整为所需值,假设您想要J,那么它将为10)。
if (e.value>=1 && e.value<=14) {
moveRow('In Shop', srcRow, srcVals, srcLastCol, spreadSheet);
destSheet.sort(1)acceding order
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.