简体   繁体   English

Google表格将行移动到新表格的脚本

[英]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. 您还可以尝试使用变量的处理方式,我只是不喜欢再次获取工作表,因此我将其中的许多函数发送给m​​ove函数。

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. 因此,让我们假设您在ifif本身之前的某个位置检索了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.

相关问题 谷歌表格脚本将完成的行移动到新工作表 - Google Sheets Script to move completed rows to new sheet Google表格脚本,用于将一个表格上的一些单元格值传递到新的表格行 - Google Sheets Script for passing a few cell values on one sheet to new sheet row Google表格将一行移动到当前行中2个单元格条件的另一张表格 - Google Sheets move a row to another sheet on 2 cell criteria in current row Google表格:根据单元格值将一行数据移动到另一张表格 - Google Sheets: Move a row of data to another sheet based on cell value 从Google Sheets脚本的Sheet中的选项卡导入A2行 - Import row A2 from tab within Sheet in Google Sheets Script 如果值存在于工作表 1 和工作表 2 中,则复制行并将其粘贴到工作表 3。Google 表格。 Google Apps 脚本 - Copy row if value exists in both sheet 1 and sheet 2 and paste it to sheet 3. Google Sheets. Google Apps Script Google表格脚本:自动将一行删除到另一张表格中 - Google Sheets Script : Automatically remove a row into another sheet 通过for循环谷歌表格脚本将行复制到另一张表格 - Copy row to another sheet via for loop google sheets script Google表格脚本onOpen(e)向表格添加新行 - Google Sheets Scripts onOpen(e) Add New Row to Sheet 我想为谷歌工作表创建一个脚本,它将制作一张工作表的多个副本并将新工作表放在活动工作表的左侧 - I want to create a script for google sheets that will make multiple copies of a sheet and place the new sheets to the left of the active sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM