简体   繁体   English

Google表格:根据单元格值将一行数据移动到另一张表格

[英]Google Sheets: Move a row of data to another sheet based on cell value

I am trying to move a row of data to another sheet based on cell value .我正在尝试根据单元格值将一行数据移动到另一张表 I use this code that i found after researching on the Internet.我使用在互联网上研究后找到的这段代码。

/**
 * Moves row of data to another spreadsheet based on criteria in column 6 to sheet with same name as the value in column 4.
*/
 
function onEdit(e) {
  // see Sheet event objects docs
  // https://developers.google.com/apps-script/guides/triggers/events#google_sheets_events
  var ss = e.source;
  var s = ss.getActiveSheet();
  var r = e.range;
   
  // to let you modify where the action and move columns are in the form responses sheet
  var actionCol = 6;
  var nameCol = 4;
 
  // Get the row and column of the active cell.
  var rowIndex = r.getRowIndex();
  var colIndex = r.getColumnIndex();
   
  // Get the number of columns in the active sheet.
  // -1 to drop our action/status column
  var colNumber = s.getLastColumn()-1;
   
  // if our action/status col is changed to ok do stuff
  if (e.value == "ok" && colIndex == actionCol) {
    // get our target sheet name - in this example we are using the priority column
    var targetSheet = s.getRange(rowIndex, nameCol).getValue();
    // if the sheet exists do more stuff
    if (ss.getSheetByName(targetSheet)) { 
      // set our target sheet and target range
      var targetSheet = ss.getSheetByName(targetSheet);
      var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
      // get our source range/row
      var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
      // new sheets says: 'Cannot cut from form data. Use copy instead.' 
      sourceRange.copyTo(targetRange);
      // ..but we can still delete the row after
      s.deleteRow(rowIndex);
      // or you might want to keep but note move e.g. r.setValue("moved");
    }
  }
}

This code works succesfully to transfer the row of data on a different tab in the same spreadsheet.此代码成功地传输同一电子表格中不同选项卡上的数据行。 But it doesnt work if i want to move that data to a diferrent spreadsheet.但如果我想将该数据移动到不同的电子表格,它就不起作用。 How can i edit that code to also works if i want to move that data row to a different spreadsheet?如果我想将该数据行移动到不同的电子表格,我如何编辑该代码也可以工作?

I need some advice!我需要一些建议! Thanks!!谢谢!!

UPDATE 1更新 1

I created that trigger code:我创建了那个触发代码:

function myFunction() {
  
}
function createSpreadsheetOpenTrigger() {
  var ss = SpreadsheetApp.getActive();
  ScriptApp.newTrigger('myFunction')
      .forSpreadsheet(ss)
      .onEdit()
      .create();
}

So i run that with granded permissions所以我以巨大的权限运行它

and then the second然后是第二个

/**
 * Moves row of data to another spreadsheet based on criteria in column 6 to sheet with same name as the value in column 4.
*/
 
function onEdit(e) {
  // see Sheet event objects docs
  // https://developers.google.com/apps-script/guides/triggers/events#google_sheets_events
  var ss = e.source;
  var s = ss.getActiveSheet();
  var r = e.range;
   
  // to let you modify where the action and move columns are in the form responses sheet
  var actionCol = 6;
  var nameCol = 4;
 
  // Get the row and column of the active cell.
  var rowIndex = r.getRowIndex();
  var colIndex = r.getColumnIndex();
   
  // Get the number of columns in the active sheet.
  // -1 to drop our action/status column
  var colNumber = s.getLastColumn()-1;
   
  // if our action/status col is changed to ok do stuff
  var targetss = SpreadsheetApp.openById("1S8KcrvzjvRxrABXa7A3W7QcLsCyag7DAHnWXgBjiaMc");
  if (e.value == "ok" && colIndex == actionCol) {
    // get our target sheet name - in this example we are using the priority column
    var targetSheet = s.getRange(rowIndex, nameCol).getValue();
    // if the sheet exists do more stuff
    if (ss.getSheetByName(targetSheet)) { 
      // set our target sheet and target range
      var targetSheet = targetss.getSheetByName(targetSheet);
      var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
      // get our source range/row
      var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
      // new sheets says: 'Cannot cut from form data. Use copy instead.' 
      var sourceData = sourceRange.getValues();
      targetRange.setValues(sourceData);
      // ..but we can still delete the row after
      s.deleteRow(rowIndex);
      // or you might want to keep but note move e.g. r.setValue("moved");
    }
  }
}

with the changes from Carlos M.随着 Carlos M.

I run that and grand permissions again.我再次运行该权限和大权限。

Am i saying it right?我说的对吗?

UPDATE 2更新 2

    function createSpreadsheetOpenTrigger() {
  var ss = SpreadsheetApp.getActive();
  ScriptApp.newTrigger('myFunction')
      .forSpreadsheet(ss)
      .onEdit()
      .create();
}

function myFunction(e) {
  // see Sheet event objects docs
  // https://developers.google.com/apps-script/guides/triggers/events#google_sheets_events
  var ss = e.source;
  var s = ss.getActiveSheet();
  var r = e.range;
   
  // to let you modify where the action and move columns are in the form responses sheet
  var actionCol = 6;
  var nameCol = 4;
 
  // Get the row and column of the active cell.
  var rowIndex = r.getRowIndex();
  var colIndex = r.getColumnIndex();
   
  // Get the number of columns in the active sheet.
  // -1 to drop our action/status column
  var colNumber = s.getLastColumn()-1;
  // if our action/status col is changed to ok do stuff
  var targetss = SpreadsheetApp.openById("1S8KcrvzjvRxrABXa7A3W7QcLsCyag7DAHnWXgBjiaMc");
  if (e.value == "ok" && colIndex == actionCol) {
    // get our target sheet name - in this example we are using the priority column
    var targetSheet = s.getRange(rowIndex, nameCol).getValue();
    // if the sheet exists do more stuff
    if (ss.getSheetByName(targetSheet)) { 
      // set our target sheet and target range
      var targetSheet = targetss.getSheetByName(targetSheet);
      var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
      // get our source range/row
      var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
      // new sheets says: 'Cannot cut from form data. Use copy instead.' 
      var sourceData = sourceRange.getValues();
      targetRange.setValues(sourceData);
      // ..but we can still delete the row after
      s.deleteRow(rowIndex);
      // or you might want to keep but note move e.g. r.setValue("moved");
    }
  }
}

I saved it and run it but it dont work.What am i doing wrong?我保存并运行它但它不起作用。我做错了什么?

UPDATE 3更新 3

That is the original spreadsheet那是原始的电子表格

. . I want to transfer that line 3 when it says ok in the F colum.当第 3 行在 F 列中显示确定时,我想转移它。 原始电子表格

That is the spreadsheet i want that data of the row to go to.那是我想要 go 的行数据的电子表格。

电子表格我希望数据转到,(TAB m1)

That is the script i wrote in the script editor in the original spreadsheet.那是我在原始电子表格的脚本编辑器中编写的脚本。

脚本

脚本

That is the log result when i click at run那是我点击运行时的日志结果

结果

It doesnt show any error but the row is not getting transfered in the other spreadsheet.它没有显示任何错误,但该行没有在另一个电子表格中传输。 Do i have some mistake in the code?我的代码有错误吗?

Solution:解决方案:

  1. You need an installable trigger to authorize edit access to two spreadsheets.您需要一个可安装的触发器来授权对两个电子表格的编辑访问权限。 You can check the documentation to create this trigger that runs on edit.您可以查看文档以创建在编辑时运行的触发器。

Sample:样本:

function createSpreadsheetOpenTrigger() {
  var ss = SpreadsheetApp.getActive();
  ScriptApp.newTrigger('myFunction')
      .forSpreadsheet(ss)
      .onEdit()
      .create();
}

Run this function manually to create the trigger.手动运行此 function 以创建触发器。

  1. Use SpreadsheetApp.openById() to open your target spreadsheet.使用SpreadsheetApp.openById()打开您的目标电子表格。 Then you can define ranges similarly to your source spreadsheet.然后,您可以定义与源电子表格类似的范围。

Sample:样本:

function myFunction(e) {
  // see Sheet event objects docs
  // https://developers.google.com/apps-script/guides/triggers/events#google_sheets_events
  var ss = e.source;
  var s = ss.getActiveSheet();
  var r = e.range;
   
  // to let you modify where the action and move columns are in the form responses sheet
  var actionCol = 6;
  var nameCol = 4;
 
  // Get the row and column of the active cell.
  var rowIndex = r.getRowIndex();
  var colIndex = r.getColumnIndex();
   
  // Get the number of columns in the active sheet.
  // -1 to drop our action/status column
  var colNumber = s.getLastColumn()-1;
  // if our action/status col is changed to ok do stuff
  var targetss = SpreadsheetApp.openById("TARGET_SS_ID_HERE");
  if (e.value == "ok" && colIndex == actionCol) {
    // get our target sheet name - in this example we are using the priority column
    var targetSheet = s.getRange(rowIndex, nameCol).getValue();
    // if the sheet exists do more stuff
    if (targetss.getSheetByName(targetSheet)) { 
      // set our target sheet and target range
      var targetSheet = targetss.getSheetByName(targetSheet);
      var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
      // get our source range/row
      var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
      // new sheets says: 'Cannot cut from form data. Use copy instead.' 
      var sourceData = sourceRange.getValues();
      targetRange.setValues(sourceData);
      // ..but we can still delete the row after
      s.deleteRow(rowIndex);
      // or you might want to keep but note move e.g. r.setValue("moved");
    }
  }
}

Replace TARGET_SS_ID_HERE with the ID from this link: https://docs.google.com/spreadsheets/d/ < ss_ID >/edit#gid=0将 TARGET_SS_ID_HERE 替换为此链接中的 ID: https://docs.google.com/spreadsheets/d/ < ss_ID >/edit#gid=0

Sample Output:样品 Output:

在此处输入图像描述

After setting 'ok' to two rows in column F:将“确定”设置为 F 列中的两行后:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google表格将一行移动到当前行中2个单元格条件的另一张表格 - Google Sheets move a row to another sheet on 2 cell criteria in current row 根据单元格值将行移动到其他工作表 - Move Row to Other Sheet Based on Cell Value Google表格根据单元格值从一张表格复制和粘贴到另一张表格 - Google Sheets Copying and Pasting from one sheet to another based on cell value 谷歌表格 - 根据特定列将行移动到另一张表格 - Google Sheets - Moving row to another sheet based on specific column Google表格将行移动到新表格的脚本 - Script for Google Sheets to Move Row to New Sheet 如何根据单元格中的值将一行从一个Google电子表格移动到另一个? - How to move a row from one google spreadsheet to another based on a value in a cell? 根据单元格值滚动一行到屏幕顶部:Google Sheet Script - Scroll a row to the top of the screen based on cell value: Google Sheet Script Google表格根据单元格编号值添加行具有值的单元格计数 - Google Sheets Add row based on cell number value the count of cell which has value 根据谷歌表中的值清除行数据(特定数据)appscript - Clear Row Data Based on Value in Google Sheet ( Specific Data ) appscript 通过Google表格中的列名将一个单元格值从一个工作表提取到另一个工作表 - Extract a cell value from one sheet to another by column name in Google Sheets
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM