[英]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 的行数据的电子表格。
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?
我的代码有错误吗?
Sample:样本:
function createSpreadsheetOpenTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger('myFunction')
.forSpreadsheet(ss)
.onEdit()
.create();
}
Run this function manually to create the trigger.手动运行此 function 以创建触发器。
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
After setting 'ok' to two rows in column F:将“确定”设置为 F 列中的两行后:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.