I would like to add a confirmation message box to appear before the script runs.
Here is the script:
function moveTo(e) {
// 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 try {
var sheetNameToWatch = "Active Discs";
var columnNumberToWatch = 15; // column A = 1, B = 2, etc.
var valueToWatch = "Completed";
var sheetNameToMoveTheRowTo = "CompletedShipped Projects";
var ss = e.source;
var sheet = ss.getActiveSheet();
var range = e.range;
if (sheet.getName() == sheetNameToWatch && range.columnStart == columnNumberToWatch && e.value == 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());
}
} catch (error) { Logger.log(error)
}
The message box would contain the following message set-up:
The message should read "Do you confirm to move Ocean Gil to Completed Shipped Projects Sheet?"
var app = UiApp.createApplication().setHeight(150).setWidth(250);
var msg = "Do you confirm to move " + "Active row cells A + B" + " to Completed Shipped Projects Sheet?";
app.setTitle("Please Confirm this Action");
app.add(app.createVerticalPanel().add(app.createLabel(msg)));
var doc = SpreadsheetApp.getActive();
doc.show(app);
}
If the person choose yes, then the "moveTo" script executes. If no, then script does nothing and closes the prompt window.
Here is the shared Google Spreadsheet.
https://docs.google.com/spreadsheet/ccc?key=0Ag8NytPhOo00dER6WG1PQ3g3V1hCbnVUaTJUZDFZenc&usp=sharing
Any help with code would be much appreciated. Thanks in Advance.
It would probably be more easy and compact to use the Browser class in this case, see doc here with example code
EDIT : in you code you can use it like this for example :
function moveTo(e) {
// 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 try {
var sheetNameToWatch = "Active Discs";
var columnNumberToWatch = 15; // column A = 1, B = 2, etc.
var valueToWatch = "Completed";
var sheetNameToMoveTheRowTo = "CompletedShipped Projects";
try{
var ss = e.source;
var sheet = ss.getActiveSheet();
var range = e.range;
if (sheet.getName() == sheetNameToWatch && range.columnStart == columnNumberToWatch && e.value == valueToWatch) {
var confirm = Browser.msgBox('Do you want to move data to the other sheet ?', Browser.Buttons.YES_NO);
if(confirm!='yes'){return};// if user click NO then exit the function, else move data
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());
}
} catch (error) { Logger.log(error)}
}
Related
For starters, I am a complete beginner at using Apps Scripts for Google Sheets at my new workplace, and we do not have an IT department who can help us create this file. I am trying to create a shared file that can be accessed by our client, our service technicians, and our main office simultaneously, so I opted to create a Google Sheet.
I am hoping to automate this process to prevent users (except myself) from needing to manually edit, move and sort data across multiple sheets that might cause the file to break or become inconsistent. For this to work, I need users to only be able to create entries in our 'main' worksheet and only view the other sheets. From here, I'd like to automate the file to move rows to specific sheets named after each unit when it's marked "DONE", and also organize each unit's sheet by the date column. Our intention is for these sheets to provide us with a running history of all the work that has been done each unit.
NOTE: I have 54 units and counting and I currently will have to copy & paste the code multiple times and edit each one to match the unit numbers. I currently have a one unit set-up, but I think there's something wrong with the code or the triggers I have set-up as it does not automatically sort the destination sheet. Either way, I think there has to be a simpler way to do this.
Any advice would be appreciated!
Here's the code that I've used thus far:
function trailer27() {
// 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 = "WORKSHEET";
var columnNumberToWatch = 6;2 // column A = 1, B = 2, etc.
var valueToWatch = "Yes";"27"
var sheetNameToMoveTheRowTo = "27"
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());
SpreadsheetApp.flush()
}
}
function Sort27() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("27")
var range = sheet.getRange("A3:F1000");
// Sorts by the values in column 1 (J)
range.sort({column: 1, descending: false});
}
function SortWorksheet(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("WORKSHEET")
var range = sheet.getRange("A3:F1000");
// Sorts by the values in column 10 (J)
range.sort({column: 1, descending: false});
}
function undoMove() {
// 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 = "27"
var columnNumberToWatch = 6; // column A = 1, B = 2, etc.
var valueToWatch = "CALLED"; "EMAIL"
var sheetNameToMoveTheRowTo = "WORKSHEET";
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());
SpreadsheetApp.flush()
}
}
If this is meant to be an onEdit function it's worth using the event object even considering the difficulty in debugging
function trailer27(e) {
const sheet = e.range.getSheet();
var sheetNameToWatch = "WORKSHEET";
var columnNumberToWatch = 6; 2 // column A = 1, B = 2, etc.
var valueToWatch = "Yes"; "27"
var sheetNameToMoveTheRowTo = "27"
if (sheet.getName() == sheetNameToWatch && e.range.columnStart == columnNumberToWatch && e.value == valueToWatch) {
var targetSheet = e.source.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(e.range.rowStart, 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(e.range.rowStart);
SpreadsheetApp.flush();
}
}
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);
}
}
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);
}
I am trying to move a row to the next tab once "Live" has been selected from a drop down menu. I just want the values and for the formatting to be disregarded.
I have had this working in the past on edit, but a recently it has decided that it it wont work unless i have a button to press on the sheet and it has also started dragging through all the data validation and conditional formatting with it. I haven't changed anything in the script, and don't think any of my colleagues have either.
Any help would be much appreciated.
function Projects(e) {
// moves whole row when "Live" is selected from drop down menu
var sheetNameToWatch = "Projects";
var columnNumberToWatch = 8;
var valueToWatch = "Live";
var sheetNameToMoveTheRowTo = "Status Tracker";
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());
}
}
Two things:
Delete the trigger and re-install it.
Instead of using moveTo(), which includes formatting, use copyTo() and include the contentsOnly option.
The modified code below should work for you. Note that I've also made some changes so that it's taking full advantage of the event object being passed to it.
function Projects(e) {
// moves whole row when "Live" is selected from drop down menu
var sheetNameToWatch = "Projects";
var columnNumberToWatch = 8;
var valueToWatch = "Live";
var sheetNameToMoveTheRowTo = "Status Tracker";
var ss = e.source; // use the event object to get the spreadsheet
var range = e.range; // use the event object to get the edited cell
var sheet = e.range.getSheet(); // use the event object to the get the sheet
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()).copyTo(targetRange, {contentsOnly:true});
sheet.deleteRow(range.getRow());
}
I use this code and while in the past it worked well, today I received this error message:
The coordinates of the target range are outside the dimensions of the sheet. (line 27, ___.gs)
My code is not too complex:
function onEdit() {
var sheetNameToWatch1 = "Assign_Page";
var columnNumberToWatch = 19;
var valueToWatch = "Delivred";
var sheetNameToMoveTheRowTo = "K_Delivery_Archive";
var sheetNameToMoveTheRowTo2 = "Buffering";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch1 && 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, 22).copyTo(targetRange, {contentsOnly: true});
var targetSheet2 = ss.getSheetByName(sheetNameToMoveTheRowTo2);
var targetRange2 = targetSheet2.getRange(targetSheet2.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, 22).copyTo(targetRange2, {contentsOnly: true});
sheet.deleteRow(range.getRow()); // line 27
var Sheet1 = ss.getSheetByName("Clients_Page");
var Sheet2 = ss.getSheetByName("Assign_Page");
var Sheet3 = ss.getSheetByName("Report");
var Sheet4 = ss.getSheetByName("Delivery_Information");
Sheet4.showSheet();
Sheet1.hideSheet();
Sheet2.hideSheet();
Sheet3.hideSheet();
}
}
What causes this error, and how can I resolve it?
It's very likely that the problem is due to
targetSheet2.getLastRow()+ 1
The proper solution will depend on several factors that are not mentioned by the OP, but here is a hint:
If the sheet has open ended formulas that return "" then the last row is the same as the maximum number of rows in the sheet and inserting more rows will not solve the problem.