我已经找到并修改了一些代码来满足我的需要,并且可以运行,但是现在它只是一种野兽,我知道有一种更好/更清洁的方式可以做到这一点-我们整天将有10-20个工作此工作表而且我只是不想让它陷入太多困境
我已附加Code.gs文件,这是指向Google工作表的链接 https://docs.google.com/spreadsheets/d/15j4sdwvQfVUrDbr_35Iof5ZV0peSu3Jm3XlWgkB7CXw/edit?usp=sharing
第1部分-此代码的这一部分是在当前用户的电子邮件以及日期和时间旁边的两列中留下一个时间戳-
function onEdit(event) {
var s = SpreadsheetApp.getActiveSheet();
//-----------------Your-Sheet 8-----------
if( s.getName() == "Your-Sheet 8" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
//-----------When data is entered in column F (the 6 column) it will automatically
// insert the current users Email in column G and will put a Time-Stamp in column H
if( r.getColumn() == 8) { //use getRow for row and getColumn for column
var nextCell = r.offset(0, 1);//offset (row,column)
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(Session.getUser());//inserts current users email
var nextCell = r.offset(0, 2);//offset (row,column)
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());//will only put date, format "123/Date and time" if time needed
}
}
//-----------------Your-Sheet 7-----------
if( s.getName() == "Your-Sheet 7" ) {
var r = s.getActiveCell();
if( r.getColumn() == 8) {
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === '' )
nextCell.setValue(Session.getUser());
var nextCell = r.offset(0, 2);
if( nextCell.getValue() === '' )
nextCell.setValue(new Date());
}
}
So on and so on
//-----------------Your-Sheet 6-----------
//-----------------Your-Sheet 5-----------
//-----------------Your-Sheet 4-----------
//-----------------Your-Sheet 3-----------
//-----------------Your-Sheet 2-----------
//-----------------Your-Sheet 1-----------
???? --------是否有一种方法可以执行-if(s.getName()== “全部”){-或getactivesheet?我只是不确定----- ????
第2部分-这是代码中的野兽部分,我真的希望有一种方法可以清理它并使它更紧凑并更好地运行。
//------------------------------------Move Row to another Sheet--------------------------------------------
var ss = SpreadsheetApp.getActiveSpreadsheet(); //Checks to see what sheet the user is on
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
//---------------Your-Sheet 1 - Moving select Row from "Your-Sheet 1" to desired Sheet--------------
// if(s.getName() == "Your-Sheet 1" ---- (checks what sheet the user is on)
// && r.getColumn() == 15 ---- (This is the Column number that is being looked at i.e. column 13 = Column M)
// && r.getValue() == "Your-Sheet 7") ---- (the value in this cell determines what or where this data is going)
if(s.getName() == "Your-Sheet 1" && r.getColumn() == 15 && r.getValue() == "Your-Sheet 7") {
var row = r.getRow(); //This is selecting the active row
var numColumns = s.getLastColumn(); //This tells how many column are in that row that have data i.e. if the last column with data is column "R" there is no need to copy the data all the way to column "Z"
var targetSheet = ss.getSheetByName("Your-Sheet 7"); // This is what sheet we want our row of data to be sent to
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1); //This checks to find the last row with data and targets the row below
s.getRange(row, 1, 1, numColumns).moveTo(target); // This Copies data to the empty row we just found
s.deleteRow(row); // This deletes the row off of the current sheet
}
if(s.getName() == "Your-Sheet 1" && r.getColumn() == 15 && r.getValue() == "Your-Sheet 3") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Your-Sheet 3");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
if(s.getName() == "Your-Sheet 1" && r.getColumn() == 15 && r.getValue() == "Your-Sheet 4") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Your-Sheet 4");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
if(s.getName() == "Your-Sheet 1" && r.getColumn() == 15 && r.getValue() == "Your-Sheet 5") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Your-Sheet 5");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
if(s.getName() == "Your-Sheet 1" && r.getColumn() == 15 && r.getValue() == "Your-Sheet 6") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Your-Sheet 6");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
if(s.getName() == "Your-Sheet 1" && r.getColumn() == 15 && r.getValue() == "Your-Sheet 8") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Your-Sheet 8");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
if(s.getName() == "Your-Sheet 1" && r.getColumn() == 15 && r.getValue() == "Your-Sheet 2") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Your-Sheet 2");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
//---------------Your-Sheet 2 - Moving select Row from "Your-Sheet 2" to desired Sheet--------------
if(s.getName() == "Your-Sheet 2" && r.getColumn() == 15 && r.getValue() == "Your-Sheet 7") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Your-Sheet 7");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
if(s.getName() == "Your-Sheet 2" && r.getColumn() == 15 && r.getValue() == "Your-Sheet 3") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Your-Sheet 3");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
if(s.getName() == "Your-Sheet 2" && r.getColumn() == 15 && r.getValue() == "Your-Sheet 4") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Your-Sheet 4");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
if(s.getName() == "Your-Sheet 2" && r.getColumn() == 15 && r.getValue() == "Your-Sheet 5") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Your-Sheet 5");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
if(s.getName() == "Your-Sheet 2" && r.getColumn() == 15 && r.getValue() == "Your-Sheet 6") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Your-Sheet 6");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
if(s.getName() == "Your-Sheet 2" && r.getColumn() == 15 && r.getValue() == "Your-Sheet 8") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Your-Sheet 8");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
if(s.getName() == "Your-Sheet 2" && r.getColumn() == 15 && r.getValue() == "Your-Sheet 1") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Your-Sheet 1");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
And i have to do this for every Sheet!
//---------------Your-Sheet 3 - Moving select Row from "Your-Sheet 3" to desired Sheet--------------
//---------------Your-Sheet 4 - Moving select Row from "Your-Sheet 4" to desired Sheet--------------
//---------------Your-Sheet 5 - Moving select Row from "Your-Sheet 5" to desired Sheet
//---------------Your-Sheet 6 - Moving select Row from "Your-Sheet 6" to desired Sheet--------------
//---------------Your-Sheet 7 - Moving select Row from "Your-Sheet 7" to desired Sheet--------------
//---------------Your-Sheet 8 - Moving select Row from "Your-Sheet 8" to desired Sheet--------------
}
答案 0 :(得分:0)
对于第一个,只要您要检查所有图纸,就应该可以省去外面的
if( s.getName() == "Your-Sheet #" )
对于第二个,请尝试以下操作:
if(r.getColumn() == 15) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName(r.getValue());
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
我在您链接的工作表上对其进行了测试,并且对我有用。
答案 1 :(得分:0)
首先,这应该分为两个问题。
对于时间戳,您不需要在每个工作表上运行代码-它会在整个工作簿中运行。另外,您正在将event
对象传递到onEdit
中,但实际上并没有使用。首先是从事件对象获取活动单元,而不是使用一堆if
语句。
更新有关编辑的信息
// To access the user email string, you have to use an installable trigger through the GUI.
function onUpdate(e) {
var sheet = e.source.getActiveSheet(); // Get the edited sheet and cells
var row = e.range.getRow();
var col = e.range.getColumn();
// If it was column H edited...
if(col == 8) {
// Set the editor's email and a timestamp
sheet.getRange(row, col).offset(0, 1, 1, 2).setValues([ [e.user.getEmail().toString(), new Date] ]);
}
}