In my small department, we currently have a simplistic ticketing system where a user fills out a google form to request service and then the ticket is added to a google sheet which we keep up on our work PCs and displays in our offices.
I'm attempting to make a script where when a checkbox is clicked, a timestamp is created on a certain column and then row on one sheet is moved over to another sheet where "Completed Tickets" are archived.
I've been able to get the script working completely with the timestamp, and I have been able to get the moving portion working completely with a moveTo function, but ONLY when you manually type in the ticket. If the ticket is submitted using Google forms, it blocks the use of the moveTo command, because it will only allow you to copy and paste formdata, and not cut and paste.
So, what I'm attempting to do is convert my 2nd onEdit script to a copyTo, rather than a moveTo. However, after many attempts at this, I can't seem to get it to function. I've posted the script I have below. Any help would be appreciated.
function onEdit(event) {
myFunction1(event);
}
function myFunction1(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
var timezone = "GMT-7";
var timestamp_format = "M-d-yyyy HH:MM:SS"; // Timestamp Format.
var updateColName = "Timestamp";
var timeStampColName = "Completed";
var sheet = event.source.getSheetByName('Form Responses 1'); //Name of the sheet where you want to run this script.
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if(s.getName() == "Form Responses 1" && r.getColumn() == 11 && r.getValue() == true) {
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
} //closes if statement operations; no close to myFunction1()
function myFunction2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Form Responses 1" && r.getColumn() == 11 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Completed Tickets");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
} //closes if statement operation
} //closes myFunction2()
myFunction2(); //calls myFunction2() so that it will run after myFunction1() code
} // closes myFunction1()
You can copy row from response sheet to target sheet then delete row from response sheet.
if (s.getName() == "Form Responses 1" && r.getColumn() == 11 && r.getValue() == true) {
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
// copy n delete
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Completed Tickets");
targetSheet.getRange(targetSheet.getLastRow() + 1, 1, 1, numColumns).setValues(s.getRange(row, 1, 1, numColumns).getValues());
s.deleteRow(row);
}
Related
So I am trying to get my onEdit script to run automatically as rows are added to my Google Spreadsheet. I already have the onEdit script working, and I have an arrayFormula set to add to new rows when added, but this isn't enough to trigger the onEdit. I can clear the contents of the column and that will trigger it, but I need to set a daily clearColumn function based on those rows marked "TRUE" and everything I try just keeps giving me an null/undefined.
**Here is my onEdit script: **
function onEdit(event) {
// assumes source data in sheet named Initial SS Addition
// target sheet of move to named Google to Mailshake
// getColumn with check-boxes is currently set to column 9 or H
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Initial SS Addition" && r.getColumn() == 9 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Google to Mailshake");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
} else if(s.getName() == "Google to Mailshake" && r.getColumn() == 9 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Initial SS Addition");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
**Here is my arrayFormula: **
=ArrayFormula(IF(ROW(H:H)=1,"Appt Done",IF(ISBLANK(H:H),"",TODAY()>=datevalue(H:H))))
**And this is my currenty clearColumn script that is giving me issues: **
function clean0() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var where = ss.getSheetByName('Initial SS Addition');
var triggerCell = where.getRange('I:I').getValue();
if (triggerCell == 'FALSE'){} else // If the cell is true do nothing
if (triggerCell == 'TRUE'){ // If the cell is false run the script
sheet.getRangeList(range).clearContent();
}
}
Any help you can offer would be greatly appreciated! TIA
Managed to figure it out:
Set up a time trigger as follows:
function createTimeTrigger() {
// Creates a trigger to run the moveRows function every hour
ScriptApp.newTrigger('moveRows')
.timeBased()
.everyMinutes(5)
.create();
}
The removed the onEdit function and changed it to just a moveRows function:
function moveRows() {
// assumes source data in sheet named Initial SS Addition
// target sheet of move to named Google to Mailshake
// getColumn with check-boxes is currently set to column 9 or H
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("Initial SS Addition");
if (!s) {
return;
}
var columnI = s.getRange("I:I"); // gets the range of cells in column I
var values = columnI.getValues(); // gets the values in the range
for (var i = 0; i < values.length; i++) {
var value = values[i][0];
if (value == true) {
var row = i + 1; // row index is 1-based
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Google to Mailshake");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
}
Now it works perfectly. Thanks to everyone for the ideas :)
I'm new to the world of javascript & google apps script and I'm struggling to get a row of data to move from one spreadsheet to another. I have it moving a row from sheet to sheet within the same spreadsheet but not across spreadsheets.
I know I need to use SpreadsheetApp.openById("insertID") rather than SpreadsheetApp.getActiveSpreadsheet() but I can't seem to make it work.
Here is the code I have that's working based on moving sheet to sheet, how do I modify it to move to a new spreadsheet?
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Timeline Submit" && r.getColumn() == 117 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("WIP");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target, {contentsOnly:true});
} else if(s.getName() == "WIP" && r.getColumn() == 117 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getColumn();
var targetSheet = ss.getSheetByName("Timeline Submit");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target, {contentsOnly:true});
}
}
I couldn't figure out everything in the second section of your code but it had too many errors so I just guessed and some of it.
Don't forget to go create a trigger for this in the Script Editor Edits/Current Project Triggers.
function installableOnEdit(e) {
var sh=e.range.getSheet();
if(sh.getName()=="Timeline Submit" && e.range.columnStart==117 && e.value==true) {
var ss=SpreadsheetApp.openById('id')
var tsh= ss.getSheetByName("WIP");
var trg=tsh.getRange(tsh.getLastRow()+1,1);
sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).copyTo(trg,{contentsOnly:true})
}else if(sh.getName()=="WIP" && e.range.columnStart==17 && e.value==false) {
var ss=SpreadsheetApp.openById('id')
var tsh=ss.getSheetByName('Timeline Submit');
var trg=sh.getRange(tsh.getLastRow()+1,1);
sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).copyTo(trg,{contentsOnly:true});
}
}
I have managed to remove a row and place in a sheet within the same Google spreadsheet. However when I modify the script to copy a row onto a different sheet, nothing happens:
function onEdit(event) {
var ss =SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "CONTRACT REGISTRY DOCUMENT" && r.getColumn() == 58 && r.getValue() == "X") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var destination = SpreadsheetApp.openById('...');
var targetSheet = destination.getSheetByName("NOT RENEWED");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
The moveTo method can only move data within the same spreadsheet. It is best practice to use range.getValues() and range.setValues() to achieve the same behavior in different spreadsheets.
function onEdit(event) {
var ss =SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "CONTRACT REGISTRY DOCUMENT" && r.getColumn() == 58 && r.getValue() == "X") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var data = r.getValues();
// please supply the destination file ID as parameter here
var destination = SpreadsheetApp.openById('...');
var targetSheet = destination.getSheetByName("NOT RENEWED");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
target.setValues(data);
s.deleteRow(row);
}
}
It seems the problem here is that you're trying to access another file from your trigger (onEdit), but that is not possible because it'd require permission to change another spreadsheet: https://developers.google.com/apps-script/guides/triggers#restrictions
According to a related question, an installable trigger is the way to go here.
In this case, you need to add something like this to your code and run it to create the trigger:
function createSpreadsheetOnEditTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger('moveRowToAnotherSheet')
.forSpreadsheet(ss)
.onEdit()
.create();
}
Based on this trigger, it is necessary to make a few changes to the original code you posted:
function moveRowToAnotherSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet(); // Changed
var r = s.getActiveRange(); // Changed
if(s.getName() == "CONTRACT REGISTRY DOCUMENT" && r.getColumn() == 58 && r.getValue() == "X") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var destination = SpreadsheetApp.openById('...');
var targetSheet = destination.getSheetByName("NOT RENEWED");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1, 1, targetSheet.getLastColumn()); // Changed
target.setValues(s.getRange(row, 1, 1, numColumns).getValues()); // Added to use setValues instead of moveTo
s.deleteRow(row);
}
}
I'm a beginner in coding and on SO, but I'm trying to understand what I'm doing and what is happening in the code.
I'm wanting to move a row between different spreadsheets.
I have a script in "CORE" Spreadsheet, which moves a row between the "Home" and "Finished" sheets Link to the spreadsheet:
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if (s.getName() == "Home" && r.getColumn() == 5 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Finished");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
else if (s.getName() == "Finished" && r.getColumn() == 5 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Home");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
I searched how to move the data located on "Finished" sheet to another spreedsheet called "DATA BASE" Link to the spreadsheet:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Finished");
var range = sheet.getRange("A1:E2");
var values = range.getValues();
var allvals = [];
for (var i = 0; i < values.length; i++) {
allvals.push(values[i]) ;
}
var dataLength = allvals.length;
// alert data
var ui = SpreadsheetApp.getUi();
// ui.alert( JSON.stringify(allvals) )
// copy to another Google Spreadsheet
var newSheet = SpreadsheetApp.openById('1Xn-hrPPr3tQwWMbKdWxHTXcazOqWu1bdHT6A5LZmlD8').getSheets()[0];
var tmp = "A1:E" + dataLength;
var newRange = newSheet.getRange(tmp);
newRange.setValues(values);
}
But I want to transfer the data from sheet "CORE"/"Finished" to sheet "DATA BASE"/ "DB" without rewriting the other rows already on the DB sheet. My idea is to make the spreadsheet "DATA BASE" a real database, where other sheets put data in it and other sheets use this data.
Is it possible to move rows between different spreadsheets like in the first code? I know its impossible to use the .openById() with the function onEdit(), but the most important thing is to move the rows without rewriting the other rows already in the target sheet (DB).
Moving a row to another spreadsheet with installable onEdit trigger
This is just an example of moving the data in a row from one spreadsheet to another with an installable onEdit Trigger. If you wish to play with it don't forget to add the target spreadsheet id.
function onMyEdit(e) {
//e.source.toast('Entry');
var sh=e.range.getSheet();
//Logger.log(JSON.stringify(e));
if(sh.getName()=='Sheet2' && e.range.columnStart==11 && e.value=="TRUE") {
//e.source.toast('Access');
e.range.setValue('FALSE');
var tss=SpreadsheetApp.openById('SSID');
var tsh=tss.getSheetByName('Sheet2');
var v=sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()-1).getValues();
Logger.log(v);
tsh.getRange(tsh.getLastRow()+1,1,v.length,v[0].length).setValues(v);
sh.deleteRow(e.range.rowStart);
}
}
I'm new to the world of javascript & google apps script and I'm struggling to get a row of data to move from one spreadsheet to another. I have it moving a row from sheet to sheet within the same spreadsheet but not across spreadsheets.
I know I need to use SpreadsheetApp.openById("insertID") rather than SpreadsheetApp.getActiveSpreadsheet() but I can't seem to make it work.
Here is the code I have that's working based on moving sheet to sheet, how do I modify it to move to a new spreadsheet?
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Timeline Submit" && r.getColumn() == 117 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("WIP");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target, {contentsOnly:true});
} else if(s.getName() == "WIP" && r.getColumn() == 117 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getColumn();
var targetSheet = ss.getSheetByName("Timeline Submit");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target, {contentsOnly:true});
}
}
I couldn't figure out everything in the second section of your code but it had too many errors so I just guessed and some of it.
Don't forget to go create a trigger for this in the Script Editor Edits/Current Project Triggers.
function installableOnEdit(e) {
var sh=e.range.getSheet();
if(sh.getName()=="Timeline Submit" && e.range.columnStart==117 && e.value==true) {
var ss=SpreadsheetApp.openById('id')
var tsh= ss.getSheetByName("WIP");
var trg=tsh.getRange(tsh.getLastRow()+1,1);
sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).copyTo(trg,{contentsOnly:true})
}else if(sh.getName()=="WIP" && e.range.columnStart==17 && e.value==false) {
var ss=SpreadsheetApp.openById('id')
var tsh=ss.getSheetByName('Timeline Submit');
var trg=sh.getRange(tsh.getLastRow()+1,1);
sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).copyTo(trg,{contentsOnly:true});
}
}