I am very new to coding with apps script but I am trying to set up a code that transfer the last row of a new google forms spreadsheet submission to another sheet in the same workbook. The reason I am trying to do this is so I can make simple calculations with the data and also edit the data (you can't edit the data with Query functions).
I have one sheet called (Form Responses 1) and one sheet called (Clients). When the form is filled out all the answers are ofcourse automatically transferred to the Form Response 1 sheet. I would like to automatically copy the last row of the Form Response 1 sheet to the Clients sheet so the information can be edited and used in calculations without losing the original submission which will be in Form Responses 1 (not a query).
There are two of the codes I tried with my very basic knowledge and they both do not work. The codes probably suck but any help and advice is greatly appreciated!
Code 1 that I tried
function onFormSubmit(e){
var ss = SpreadsheetApp.getSheetByName("Form responses 1")();
var copyFromRange = 'Form responses 1!A2:M999';
var copyToRangeStart = 'Clients!A2:M999';
copyValuesOnly(copyFromRange, copyToRangeStart);
}
Code 2 that I tried
function onFormSubmit(e){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('Form responses 1');
var r = s.getRange(s.getLastRow(),s.getLastColumn());
var data = r.getValues();
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Clients");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
targetSheet.getRange(targetSheet.getLastRow()+1,1,1,20).setValues(data);
I have been browsing the website while trying to figure this out and learn more on scripting, you have all been a great help!
function onFormSubmit(e){
var ss=SpreadsheetApp.getActive();
ss.getSheetByName('Clients').appendRow(e.values);
}
onFormSubmit Event Object
You don't want to get the last row because if submissions come to fast the last row may not be the one from the currrent onFormSubmit trigger.
Related
I have a google script that copies the last row on that spreadsheet to another spreadsheet sheet every time an entry is made. This entry is not linked to a Google form at all - so it is not bound to an onFormSubmit trigger - this submission is made from a JotForm form and inserted into the spreadsheet automatically. What I would like to know if my code will always work even if say two users make their submissions at the same time.
The code works fine - there seems to be no problem with that. I use the On change trigger. But I'd like to make sure it would always work. Many thanks for your help in advance.
function copyLastRow() {
var target = SpreadsheetApp.openById('xxxxx').getSheetByName('Records');
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var lastrow = sheet.getLastRow();
var sourceData = sheet.getRange(lastrow, 1, 1, 10).getValues();
target.appendRow(sourceData[0]);
}
This is kind of the template I imagine you could use in completing this objective.
function updateSpreadsheet() {
//Open Form Responses Spreadsheet and get the Sheet you are reading data from
// You will need to update ID and SheetName parameters for your own sheets.
var formResponses = SpreadsheetApp.openById(ID).getSheetByName(SheetName);
//Open destinationSheet that you are writing to and get the sheet that you are writing to
// You will need to update ID and SheetName parameters for your own sheets.
var destinationSheet = SpreadsheetApp.openById(ID).getSheetByName(SheetName);
//Getting last rows from formResponse and destinationSheet
//It is not totally necessary to create everything as a variable, but I wanted to make it explicit what I am doing here
var formResponsesLastRow = formResponses.getLastRow();
var destinationSheetLastRow = destinationSheet.getLastRow();
//We are going to calculate the difference between the formResponse and destinationSheet
//This lets us know how many more rows have been added since we last updated destinationSheet
var numberNewRows = formResponsesLastRow - destinationSheetLastRow;
//Now get the new data to Write
var dataToWrite = formResponses.getRange((formResponsesLastRow - numberNewRows) + 1 , 1, numberNewRows, 3).getValues();
//Write to destinationSheet
destinationSheet.getRange(destinationSheetLastRow + 1, 1, numberNewRows, 3).setValues(dataToWrite);
}
I'm attempting to write a script to continuously update two spreadsheets so that the information put in one always goes into the other.
The full project is to create a master spreadsheet with 50 people on it and each one of them has their own sheet/page, however they aren't allowed to access this master sheet as they are not allowed to see the other peoples data. (I am currently unaware of any viewing permission commands but I'm pretty sure there aren't any).
So the solution that I have been considering is creating 50 other spreadsheets and each of them will have a single sheet with all of the same information as the master Spreadsheet and the name of the sheet will be the same as on the master page.
I found this code on another post that was for a similar problem
function onEdit(e) {
// Get the event object properties
var range = e.range;
var value = e.value;
//Get the cell position
var row = range.getRowIndex();
var column = range.getColumnIndex();
exportValue(row,column,value)
}
function exportValue(row,column,value) {
var ss = SpreadsheetApp.openById(targetID);
var s = ss.getSheetByName(targetSheetName);
var target = s.getRange(row, column);
target.setValue(value);
}
in this I understand what everything is doing except the ("var range = e.range; var value = e.value;") lines, is ths what im looking for or not, idk
Thanks in advance
:)
I'm quite new to google sheet script and looking for some assistance. I am trying to make a google script that will erase cells in a response sheet after a user has submitted it. As this could be a form edit url, it may not necessarily be the last row.
So everytime a form is submitted / edited, columns AN:AQ are cleared.
Any help greatly appreciated
I'm adding the working script to OP's question for anyone who comes across this.
function onFormSubmit11(e) {
var range = e.range;
var ss = range.getSheet();
var row = range.getRowIndex();
ss.getRange("AN"+row).clear();
ss.getRange("AO"+row).clear();
ss.getRange("AP"+row).clear()
ss.getRange("AQ"+row).clear()
}
The Spreadsheet trigger invoked by the Google Form contains the Range object. You can retrieve the sheet linked to the range as well the index of the row linked to the current form submission.
function formTrigger(e) {
var range = e.range;
var sheet = range.getSheet();
var rowIndex = row.getRowIndex();
sheet.getRange(rowIndex + 1, 1).setValue("Hello");
}
You could try something as simple as an onFormSubmit running the script through your linked spreadsheet. The script below will clear AN:AQ every time a form is submitted which is 'alf of what I think you're asking for.
As for on edit, that's something I'm trying to figure out myself in my own question!
function onFormSubmit() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sh = ss.getActiveSheet()
sh.getRange('AN1:AQ').clear();
}
Hi Again,
Try this below. As before, make sure your trigger is set on OnFormSubmit and it should work flawlessly. I've tested it myself, it only cleared the range between AN:AQ on row (wherever the form edits). If you want to delete different sections just replace the AN:AQ for whatever column(s) you wish to clear.
function onFormSubmit(e) {
var range = e.range;
var ss = range.getSheet();
var row = range.getRowIndex();
ss.getRange("AN:AQ"+row).clear();
}
I have the following app script associated with a Google Spreadsheet that is accepting data from a Google Form:
function writePatientData() {
var spreadsheet = SpreadsheetApp.openById("<spreadsheet id>");
var sheet = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
//get last row in active/main sheet
var numRows = sheet.getLastRow();
//get last row of data
var last_row = sheet.getSheetValues(numRows, 1, 1, 23);
//get patientID (column V) in last row of sheet
var lastPatientID = sheet.getRange(numRows,3).getValue();
//find patient sheet based on patientID and make it active, then write to it
var patientSheet = SpreadsheetApp.getActive().getSheetByName(lastPatientID);
var activePatientSheet = SpreadsheetApp.getActive().getSheetByName(lastPatientID);
activePatientSheet.getRange(activePatientSheet.getLastRow()+1, 1,1,23).setValues(last_row);
}
What this script is doing is writing data (a row) to another sheet within this spreadsheet based on the the patientID (column V). This works as it should when I manually run the script. However, when I set a trigger to run this script (either onSubmit or edit) nothing happens. I created another function that just writes a message to the logs and set a trigger for that function and it works, so I think there is something in the script that is causing it to fail. Any ideas appreciated.
There are a few issues with your code. I tried to fix it while commenting each line I changed. Hopefully that is clear enough, please comment if you have any questions and I'll try to clarify.
function writePatientData() {
var spreadsheet = SpreadsheetApp.getActive(); //no need for id if the script is on the same spreadsheet
//var sheet = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
//setActiveSheet will not work from a trigger like on-form-submit (what if no-one has the sheet open, or multiple have)
var sheet = spreadsheet.getSheets()[0]; //if you want the first sheet, just get it, no need to "activate"
var numRows = sheet.getLastRow();
var last_row = sheet.getSheetValues(numRows, 1, 1, 23)[0]; //added [0] since it is just one row
//var lastPatientID = sheet.getRange(numRows,3).getValue(); //you already have this in memory
var lastPatientID = last_row[2]; //arrays are zero based, that's why 2 instead of 3
//btw, you mention column V, but this is actually C
//var patientSheet = SpreadsheetApp.getActive().getSheetByName(lastPatientID);
//you already have the spreadsheet, no need to get it again
var patientSheet = spreadsheet.getSheetByName(lastPatientID);
//var activePatientSheet = spreadsheet.getSheetByName(lastPatientID); //this is the exact same as above, why?
patientSheet.appendRow(last_row); //appendRow is just simpler than getRange(getLastRow).setValues
}
I want the sheet that collects data from a google form to delete old data. I have this script in the sheet but it doesn't seem to run when new data is added via the form.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("1054");
var datarange = sheet.getDataRange();
var lastrow = datarange.getLastRow();
var values = datarange.getValues();// get all data in a 2D array
var currentDate = new Date();
var oneweekago = new Date();
oneweekago.setDate(currentDate.getDate() - 7);
for (i=lastrow;i>=2;i--) {
var tempdate = values[i-1][0];
if(tempdate < oneweekago)
{
sheet.deleteRow(i);
}
The sheet it is linked to is called 1054.
Do I need to put appscript in the Form? If so, what would that be?
I think it would be better to leave the sheet and the form it is attached to alone. Here's what I do on a form that's on my website.
function formSubmitEvent(e)
{
var ss=SpreadsheetApp.openById('SpreadsheetID');
var sht=ss.getSheetByName('ResponseReview');//this is another sheet I used for reviewing the responses.
sht.appendRow(e.values);//I append a row to the ResponseReview sheet
selfEmail(e.values);//and I send myself an email if my quota is above 75
}
Even if you delete old data on the linked sheet the form will continue adding data where it left off because evidently it keeps track of what the next row is.
In general, I leave the linked sheet alone. It is true that I built my form in Googgle Apps Script so at some point when I get a lot of responses then I will unlink the form and delete both and rebuild the form by running that script and a new form and sheet will be linked but I will continue my review process in the other sheet which also has the capability to create a Google Doc from the submitted data. In essence, it doesn't really matter to me where the form wants to put the data because I capture the onFormSubmit trigger and I put into my ResponseReview sheet from the values array. And I can do anything I want with that sheet without affecting the linkage.