I am trying to write a script that will delete all responses, start a new sheet and then style the new sheet by making the first row red color. Here is what I have so far:
function Initialize2() {
var form = FormApp.getActiveForm();
var ss = SpreadsheetApp.openById(form.getDestinationId());
form.deleteAllResponses();
form.removeDestination();
form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId());
form = FormApp.getActiveForm();
ss = SpreadsheetApp.openById(form.getDestinationId());
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A1:Z1");
range.setBackground("red");
sheet.setFrozenColumns(1);
}
The problem that I am facing is that it will create a new sheet, but the color update happens on the previously active sheet. How do I make it so that it updates the new active sheet?
By the time you select a sheet for color changes, the new sheet with form responses may not exist yet, because Sheets may postpone operations to improve performance. To ensure the creation has happened, call
SpreadsheetApp.flush();
before
var sheet = ss.getSheets()[0];
Related
I have a jot form that submits data to one dedicated google sheet (DGS). I work primarily with one google sheet doc that copies cell data from the DGS. I'm trying to implement a code that when new data is entered to duplicate data and send to the google sheet doc I use regularly.
*Note. If I try to alter the DGS, it throws an error and distorts the data sent.
function copydata()
{
var sourceSheet = SpreadsheetApp.openById("Google Sheet ID").getSheetByName("Tab Sheet Name");
var targetSheet = SpreadsheetApp.openById("Google Sheet ID").getSheetByName("Tab Sheet Name");
var rangeToCopyFrom = sourceSheet.getRange(sourceSheet.getLastRow(), 2, 1, 15);
var rangeToPasteTo = targetSheet.getRange(targetSheet.getLastRow(),1,1,15);
var rangeToCopyFrom = sourceSheet.getRange("A50:N50");
var rangeToPasteTo = targetSheet.getRange("A4:N4");
rangeToCopyFrom.copyTo(rangeToPasteTo, {contentsOnly:true});
}
This could be us to append data to another spreadsheet:
function onFormSubmit(e) {
const dss = SpreadsheetApp.openById("id");
const sh = dss.getSheetByName("Destination Sheet Name");
sh.appendRow(e.values);
}
It needs to go into DGS. I'd need to know more about the date to determine if it's not duplicate data
I'm using a spreadsheet to track my routine in the gym. I have it set up to duplicate the active sheet and name the duplicate with today's date. It's set up to trigger the below code on edit:
function myProgress() {
var fullDate = Utilities.formatDate(new Date(), "GMT-5", "dd/MM/yyyy");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var name = fullDate;
var currentName = ss.getSheetByName(name);
if (!currentName) {
ss.insertSheet(name, {template: sheet});
ss.moveActiveSheet(1);
ss.setActiveSheet(ss.getSheets()[0]);
}
}
Which works great on desktop.
If today (28/02/2020) I edit my workout routine done on 25/02/2020, Sheet 25/02/2020 is duplicated and named "28/02/2020". Then Sheet 28/02/2020 is the active sheet.
But because of the way the mobile app works (maybe to do with the confirmation of entered text?) it will duplicate and rename the the sheet but it won't set the new one as the active sheet.
Yes, it's only a minor inconvenience to have to remember to go to today's sheet before continuing to edit, but I'd really love to work out a way around this.
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 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 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.