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.
Related
I have multiple Google Forms set up to take in data (just numbers). That data is then grabbed using an onFormSubmit function with a manually programmed trigger. Is there a way I can use just one trigger for multiple forms and have them go to the appropriate spreadsheet?
For example, I have a form called dry storage and another called freezer (and have 6 other forms and sheets), with corresponding sheets in a spreadsheet. Is there a way that when the form is submitted, that it takes the data, and places it in the correct spreadsheet? I have it working when I manually type in the sheet name, but I would like it to be done dynamically in case more sheets and forms need to be added so a new function and trigger doesn't have to be created every time.
The forms created have already been linked to the spreadsheet. I basically want every linked form submitted to get the formID of the form it was submitted, and be able to match it to the corresponding sheet name.
In a perfect world, the spreadsheet is determined by the form it is submitted on by name. Is it possible to do this dynamically?
I would like to not have 8 triggers with 8 functions that all do the exact same thing, with the only difference is the formID and corresponding sheet being passed.
The onSubmit function does exactly what I want it to do for one form and one spreadsheet, but I would like it to be able to take in any form and map it to the correct sheet.
The onFormSubmit function was me attempting to use events to generalize it, but with no avail, as I am unfamilar with how events work. I've searched high and low for an example that matches what I am trying to do on the Google website, and StackOverflow. Any examples or points on how events work would be beneficial as well.
Thank you!
function onFormSubmit(e)
{
var res = e.values // get the data from the form
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Dry Storage"); // manually get the sheet it should be going to, I'd like this to be dynamic
for (var a = 0; a < res.length; a++)
{
sheet.getRange(7,(a+1)).setValue(e.values[a]) // set the form values in the sheet
}
function trigger()
{
var form = FormApp.openById(DryStorageFormID) // formID for dry storage. is there a way that this ID can be grabbed dynaically from the submitted form?
ScriptApp.newTrigger('onSubmit')
.forForm(form)
.onFormSubmit()
.create();
}
function onSubmit()
{
var ss = SpreadsheetApp.getActiveSpreadsheet(); // get active spreadsheet
var targetSheet = ss.getSheetByName("Dry Storage"); // data should be sent to this sheet. I would like this info to be somehow passed to the function instead of have it hard-coded.
var form = FormApp.openById(DryStorageFormID); // DryFormID
try{
var formResponses = form.getResponses();
}
catch(e)
{
console.error("No Data!");
}
var itemResponses = formResponses[formResponses.length - 1].getItemResponses();
for (var j = 0; j < itemResponses.length; j++)
{
var itemResponse = itemResponses[j];
var offset = j + 2
targetSheet.getRange(offset, (FR.order +1)).setValue(itemResponse.getResponse())
}
}
The easier way to proceed, instead of having one spreadsheets for each form, is to have one spreadsheet that receive responses from multiple forms.
Besides the above, you could send the received responses to another spreadsheet using the same on form submit trigger or by other means (i.e. IMPORTRANGE).
To get the sheet receiving the form response you could use the following code line (assuming that e is the spreadsheet on form submit object):
var sheet = e.range.getSheet();
then you could use sheet.getName() to get the sheet name or whatever else you need of that sheet.
To get the "name" of the form that triggered the on form submit trigger you could use:
var url = sheet.getFormUrl();
var form = FormApp.openFormByUrl(url);
var title = form.getTitle();
Related
Identifying Form destination (Spreadsheet AND SHEET)
How to get a form linked to a sheet using google app script?
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.
I'm setting up a Google Sheet that is connected to a Form. The Form is bringing in data from unique users. When a new user completes the form, and the data is stored in the Form Responses 1 tab, I want a new tab to be created that is named with the name of the new user. Additionally, I want to copy in some data to this newly created tab from two other, separate tabs... one row from the Form Responses 1 tab, and a large range of data and formulas from another tab (right now I have that tab named "Sheet 5").
I'm able to create a new tab and name it based on the unique user that shows up in the Form Responses 1 tab. However, I don't know how to copy the data from those other tabs into the newly created tab because I'm trying to use getSheetbyName, which requires me to provide a named sheet. Well, I don't have a named sheet because the target sheet to be copied into will always be based on new data that comes into the form.
Any help is appreciated. Thanks.
function onFormSubmit(){
Logger.log('submit ran');
var form = FormApp.openById('1oaGxmsd8SEDJ9HrXixpriCeKYrRxr1ZVX0x1zbohTIQ');
ScriptApp.newTrigger('onFormSubmit')
.forForm(form)
.onFormSubmit()
.create();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Form Responses 1');
//Get last row of data
var lastRow = sheet.getLastRow();
var colB_Data = sheet.getRange(lastRow, 2).getValue();
//var thisUser = 'theUserName';
ss.insertSheet(colB_Data);
};
function copyTo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Sheet5");
var pasteSheet = ss.getSheetByName("John Doe");
var source = copySheet.getRange(1,1,4,2);
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,4,2);
source.copyTo(destination);
The onFormSubmit function works, but the rest of the code is not working. I can't get data copied into a sheet that doesn't exist yet. It seems like I'd need the "John Doe" to be a variable that copies into each new sheet that is created
Try this:
function onFormSubmit(e){
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet5');
var rg=sh.getRange(1,1,4,2);
var vA=rg.getValues();
var username=e.values[1];
var nsh=ss.insertSheet(username);
nsh.getRange(1,1,vA.length,vA[0].length).setValues(vA);
}
This code is written using the Spreadsheet Script Editor. And you have to create a onformSubmit trigger in the same project for the Spreadsheet.
onFormSubmit Trigger for Spreadsheets
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();
}