Google表格的脚本将行移至新工作表

时间:2016-10-05 19:15:37

标签: javascript google-sheets

我正在尝试设置的商店时间表存在一些问题。当某些数据输入“J”列时,我希望将整行移动到相应的新选项卡中。我已经为我创建的样本表添加了一个可编辑的链接,这也有助于解释我想要做的事情。

如果可能的话,也可以将背景更改为样本表上的颜色。

谢谢!

可编辑链接:https://docs.google.com/spreadsheets/d/1D0iptPchrrLuq75ysvXwisBa_Xr0fuNabAdTb3sm-yA/edit?usp=sharing

1 个答案:

答案 0 :(得分:0)

这是一个粗略的代码可以使用,但是如果你将值更改为相同的值(因此,如果它已完成,并且你将其编辑为更改为20),它将只删除行。这很容易解决,但我现在没有时间:)

编辑:记得要注意,它会使用最后一行,所以为了将内容移回约会你应该将图例移到工作表的顶部:)或者你应该摆脱回到约会一点一点。你也可以玩变量如何使用,我只是不喜欢再次抓取纸张,所以我将很多它们发送到移动功能。

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 回答评论

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

但是,对于任何运行时间较长的较大脚本来说,这都是不好的做法。然后,您每次都会浪费时间访问SpreadsheetApp。

至于颜色,它是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()
}

至于排序,这是在调用moveRow()之前需要destSheet = spreadSheet.getSheetByName(destName);的地方。下面,我们假设您在if之前或if本身之前的某处检索了destSheet。此示例将按列A按升序对其进行排序(因此将1调整为您需要的值,让我们说您想要J,那么它将是10)

  if (e.value>=1 && e.value<=14) {
    moveRow('In Shop', srcRow, srcVals, srcLastCol, spreadSheet);
    destSheet.sort(1)acceding order
  }