Google表格脚本 - 将行从一个标签移动到另一个标签

时间:2017-08-05 21:29:44

标签: database google-sheets macros rows move

我试图修改以下论坛帖子中的代码以帮助满足我的需求:

https://productforums.google.com/forum/#!topic/docs/ehoCZjFPBao/discussion

function onEdit() {
  // moves a row from a sheet to another when a magic value is entered in a column
  // adjust the following variables to fit your needs
  // see https://productforums.google.com/d/topic/docs/ehoCZjFPBao/discussion

  var sheetNameToWatch = "Transfers";

  var columnNumberToWatch = 15; // column A = 1, B = 2, etc.
  var valueToWatch = "yes";
  var sheetNameToMoveTheRowTo = "Archive";

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getActiveCell();


  if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {

    var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
    var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
    sheet.deleteRow(range.getRow());
  }
}

当输入特定值时,此代码执行的操作是将一行从一个选项卡移动到另一个选项卡(在上面的代码中,它会从"转移"到"存档"当第15栏(列O)中输入“" yes"”时。

我的需求有点不同。我不能让用户一次移动一行,因为可能涉及多行并将它们组合在一起,它们需要满足某些条件(例如,所有行中的金额均衡为0,是否使用相同的帐户等) 。我相信这是一个基本的数据验证。

因此,我尝试制作一个公式,这样如果我在单元格F8中输入单词yes,它会填充单词" Send" (以前是"是",为了清楚起见而改变)到列O.如果在单元格F8中输入单词yes,则每行都带有单词" Send"应该移动到存档列。问题是,上面的代码依赖于一个名为OnEdit的函数,并使用单词Send via formula填充行不会触发脚本运行。上面的代码需要用户手动输入正确的关键字才能移动行。

有人可以帮助修改或重写代码,以便用户手动输入关键字" yes"在单元格F8中,然后让它移动任何包含单词" Send"由O列中的公式填充?

以下Google表格是我尝试做的简单示例。其他评论可以在" MASTER - DO NOT EDIT"标签

https://docs.google.com/spreadsheets/d/1iajS90qvwEOGVnl2lpDbVtcI532OO8n4NLZEBDUpVzA/edit#gid=398066315

谢谢你的期待。如果有人需要其他信息,请告诉我。

~~~~~~~~~~~~~~~~~~~~~~~~~~~

编辑:我在下面尝试过Cooper的代码,它似乎有效。我是这样的新手,但我设法添加了一些东西让它在onEdit上运行,并删除了初始触发词。

这个稍微调整过的代码运行示例如下:

function onEdit() {
  archiveRows();
  clearCells();

}

function archiveRows()
{

/*installable trigger rows 10-13 https://developers.google.com/apps-script/guides/triggers/installable
 if(!projectTriggerExists()
 {
   ScriptApp.newTrigger('myFunction')
    .forSpreadsheet(ss)
    .onOpen()
    .create();
 }//by wrapping the trigger creation like this you don't have to worry about creating unwanted triggers.
*/  

  var ss=SpreadsheetApp.getActive();
  var sh0=ss.getSheetByName('Transfers'); //sh0 = Transfers tab
  var rg0=sh0.getDataRange(); //rg0 = Range of sh0, This is functionally equivalent to creating a Range bounded by A1 and (Range.getLastColumn(), Range.getLastRow()). https://developers.google.com/apps-script/reference/spreadsheet/sheet#getdatarange
  var sh1=ss.getSheetByName('Archive'); //sh1 = Archive tab
  var vals=rg0.getValues(); //Returns the rectangular grid of values for this range. Returns a two-dimensional array of values, indexed by row, then by column. The values may be of type Number, Boolean, Date, or String, depending on the value of the cell. Empty cells will be represented by an empty string in the array. Remember that while a range index starts at 1, 1, the JavaScript array will be indexed from [0][0]. https://developers.google.com/apps-script/reference/spreadsheet/range#getvalues
  for(var i=vals.length-1;i>11;i--) //When deleting rows it is better to start from the bottom otherwise deleted rows will mess up your loop indexing
  {
    if(vals[i][14]=='SEND') //If column 14 in the range has the word 'SEND' then run the next lines of code. 14 is column O. Column 0 is 15, but see note above regarding JavaScript array being indexed from [0][0]
    {
      sh1.appendRow(vals[i]);
      sh0.deleteRow(i+1);//indexes start at zero but rows start at one
    }

  }

}
//This could be accomplished with SpreadsheetApp.getActive().getActiveSheet().clear();
function clearCells()
{
  //https://stackoverflow.com/questions/9268570/i-need-a-button-to-clear-cells-in-a-google-spreadsheet
  var ss=SpreadsheetApp.getActive();
  ss.getRange('A1:A1').clearContent()
}

function projectTriggerExists(functionName)
{
  if(functionName)
  {
    var allTriggers=ScriptApp.getProjectTriggers();
    var funcExists=false;
    for(var i=0;i<allTriggers.length;i++)
    {
      var trigger=allTriggers[i];
      if(allTriggers[i].getHandlerFunction()==functionName)
      {
        funcExists=true;
        break;
      }
    }
  }
  return funcExists;
}

电子表格的本质是我需要复制&#34;转移&#34;原始帖子中的标签十几次,以便一次可以由多个人使用(用户输入的数据基本上超过12&#34;表格&#34;)。所有的数据都放入了这些数据中。转移&#34;标签应该用尽相同的&#34;存档&#34;标签。但是,我还想构建一个基本的路由系统,这样不同的代码字就不会让所有内容都路由到一个标签,而是将这些行发送给不同的&#34;存档&#34;标签。例如,&#34; OK + Send + Staff1&#34;将该行发送到名为Staff1Archive的选项卡,&#34; OK + Send + Staff2&#34;发送到名为Staff2Archive的标签,&#34; OK + Send + Staff3&#34;发送到名为Staff3Archive等的标签

任何人都可以帮助使用此代码吗?

1 个答案:

答案 0 :(得分:1)

我认为这会为你做到。

function archiveRows()
{
  var ss=SpreadsheetApp.getActive();
  var sh0=ss.getSheetByName('Transfers');
  var rg0=sh0.getDataRange();
  var sh1=ss.getSheetByName('Archive');
  var vals=rg0.getValues();
  for(var i=vals.length-1;i>11;i--)
  {
    if(vals[i][14]=='SEND')
    {
      sh1.appendRow(vals[i]);
      sh0.deleteRow(i+1)
    }
  }
}