I've setup a script to copy and process a spreadsheet that is generated from a form.
I gather the responses like this:
var responses = formResponses.getDataRange().getValues();
Then accessing the individual columns through the array index, for example:
var timestamp = row[0];
var agentName = row[1];
Now, while this works. Any changes to the form could end up screwing the whole thing up and the array indices will change.
Can I instead query by column name?
If accessing it via the onFormSubmit event is not viable for you because you need to access the data post-submission, I have created code that allows you to access via column name.
function getDataFields(workingRow){
//header is row 1 -- not row 0
var headerRow = 1;
var headerRowValues = getRowRange(headerRow).getValues();
var workingRowValues = getRowRange(workingRow).getValues();
var dataFields = [];
for (var colNum in headerRowValues[0]){
dataFields[headerRowValues[0][colNum]] = workingRowValues[0][colNum];
}
dataFields['Working Row'] = workingRow;
return dataFields;
}
function getRowRange(workingRow){
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lastColumn = activeSheet.getLastColumn();
return activeSheet.getRange(workingRow, 1, 1, lastColumn);
}
You can then use dataFields['Column Name'] to access the data of that particular row.
if you use the onFormSubmit event you can access all form values by their named values, this is explained in the documentation on Spreadsheet Form Submit Events so you won't have this problem anymore...
Related
My code here:
function script1(){
//What is the form and data item ID?
var form = FormApp.openById("Form ID");
var namesList = form.getItemById("Item ID").asListItem();
//What sheet is used to update the form?
var ss = SpreadsheetApp.getActive();
var names = ss.getSheetByName("Update");
// Grab the values in the sixth column of the sheet - use 2 to skip header row
var namesValues = names.getRange(2, 6, names.getMaxRows() - 1).getValues();
var departmentNames = [];
// Convert the array ignoring empty cells
for(var i = 0; i < namesValues.length; i++)
if(namesValues[i][0] != "")
departmentNames[i] = namesValues[i][0];
// Populate the drop-down
namesList.setChoiceValues(departmentNames);
}
appears to triggers the form to submit causing problems. I have an onFormSubmit trigger set up to copy the data to a database and then delete the row. Unfortunately, the headers keep getting copied to the database since the form does not actually send any data and the only row with data is the headers. How can I update the form without the form submitting? Screenshot example is included below.
[
I hope I was able to make myself clear. Thank you for any help.
Modified Screenshot
Here is a screenshot of the trigger and its executions.
Sheet1
Sheet2
I'm wondering if anyone could help me create a appscript for terminal invoicing and database purposes? When I click "SUBMIT" as a button in sheet 1 image, it must reflect line by line like in Sheet 2 image. (see sheet 1 image and sheet 2 image)
-after clicking submit it sheet 1 must auto clear
-when I input another set of data after clearing it needs to keep going down line per line in sheet 2
You can use the following code for that:
function myFunction() {
// declarations
var ss = SpreadsheetApp.getActive();
var sourceSheet = ss.getSheetByName('YOUR_SOURCE_SHEET_NAME');
var database = ss.getSheetByName('YOUR_DATABASE_SHEET_NAME');
// obtain common variables for each item
var dateOfOrder = sourceSheet.getRange('B1').getValue();
var dateOfDelivery = sourceSheet.getRange('B2').getValue();
var agent = sourceSheet.getRange('B8').getValue();
var customer = sourceSheet.getRange('B3').getValue();
// compute variables to initiate reading item rows
var rows = [];
var lastItemRow = sourceSheet.getLastRow();
var firstItemRow = 11;
var nItems = lastItemRow - firstItemRow + 1;
if (nItems < 1) return;
// get row-level information and append to database sheet
var itemNames = sourceSheet.getRange(firstItemRow, 1, nItems, 1).getValues();
var itemQuantities = sourceSheet.getRange(firstItemRow, 2, nItems, 1).getValues();
var itemPrices = sourceSheet.getRange(firstItemRow, 3, nItems, 1).getValues();
for (var i=0; i<nItems; i++) {
var itemName = itemNames[i][0];
var itemQuantity = itemQuantities[i][0];
var itemPrice = itemPrices[i][0];
database.appendRow([dateOfOrder, dateOfDelivery, agent, customer, itemName,
itemQuantity, itemPrice, itemQuantity * itemPrice]);
}
// clear source sheet
sourceSheet.getRange("B1:B8").clear();
sourceSheet.getRange("A11:H").clear();
}
The idea is to first obtain data from your source sheet (using getRange() along with getValue() or getValues()) and afterwards insert it using appendRow().
Finally, you can clear the range using the clear() method.
In order to create a button that calls this script, I suggest you check out this (Google spreadsheet - Making buttons that add data into cells into another sheet when pressed) other Stackoverflow answer. When assigning the script to the image, the name of it should be "myFunction" (without the quotes).
So I have a google form feeding into a sheet, and from the spreadsheet I'm making google docs from a template with the answers in the spreadsheet.
The form has the ability to save mid way and come back later via a custom question asking if they want to save and comeback (this submits the form). The script on my spreadsheet activates onFormSubmit so when they quit, the template gets created with half their answers. When they eventually come back and finish it off, I want the script to know where to create the template from.
For instance, 5 more rows were added since they quit and the script creates the template from a manual change of the line 'var tactics' by changing the numbers to the row. e.g. if I was about to test another entry, I'd change the numbers to the next empty row first, then when the form is submitted, it would use that row. Not practical. It wouldn't work.
I've looked around a bit and found onEdit(e) but that doesn't work unless it's a manual entry.
My question is, is there another way other than onEdit to find the last cell UPDATED, not ADDED else it'll grab the last row in the sheet, which I don't want. If it grabs the last cell updated then it will grab the correct row to run the script for. I'll add my script to the bottom if it'll help. ID's etc. have obviously been removed.
Any ideas?
function onFormSubmit(e) {
var Sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var headers = Sheets.Spreadsheets.Values.get('myID', 'A1:U1');
var tactics = Sheets.Spreadsheets.Values.get('myID', 'A6:U6');
var templateID = "myID"
for(var i = 0; i < tactics.values.length; i++){
var Timestamp = tactics.values[i][0];
var IDCFREF = tactics.values[i][2];
var raisedby = tactics.values[i][4];
var AccMan = tactics.values[i][6];
var Contrib = tactics.values[i][7];
var Contract = tactics.values[i][8];
var CompName = tactics.values[i][9];
var ValidFrom = tactics.values[i][10];
var ValidTo = tactics.values[i][11];
var Freq = tactics.values[i][12];
var PDetailFreq = tactics.values[i][13];
var BillType = tactics.values[i][14];
var TypeOfRebate = tactics.values[i][15];
var RebateDetails = tactics.values[i][16];
var RTarget = tactics.values[i][17];
var GiveDeets = tactics.values[i][19];
var WhyGiveRebate = tactics.values[i][20];
var documentID = DriveApp.getFileById(templateID).makeCopy().getId();
DriveApp.getFileById(documentID).setName('Rebate ' + IDCFREF + ' Request');
var body = DocumentApp.openById(documentID).getBody();
var header = DocumentApp.openById(documentID).getHeader();
header.replaceText('##IDCF##', IDCFREF)
body.replaceText('##REF##', IDCFREF)
body.replaceText('##RAISED##', raisedby)
body.replaceText('##ACCMAN##', AccMan)
body.replaceText('##CONTRIB##', Contrib)
body.replaceText('##SIGNED##', Contract)
body.replaceText('##NAME##', CompName)
body.replaceText('##FROM##', ValidFrom)
body.replaceText('##TO##', ValidTo)
body.replaceText('##FREQ##', Freq)
body.replaceText('##BESPOKE##', PDetailFreq)
body.replaceText('##BILL##', BillType)
body.replaceText('##TYPE##', TypeOfRebate)
body.replaceText('##DEETS##', RebateDetails)
body.replaceText('##TARGET##', RTarget)
body.replaceText('##FULL##', GiveDeets)
body.replaceText('##ELAB##', WhyGiveRebate)
}
}
So for anyone who has the same issue, here was my solution:
function onFormSubmit(e) {
var range = e.range;
var ss = range.getSheet();
var row = range.getRowIndex();
var tactics = Sheets.Spreadsheets.Values.get('ID', "A:AQ"+row);
The way it works is just delete every row in the form responses sheet up until the last entry, and then voila. ("A:AQ"+row) gets the row index from the variable 'row' created from 'e.range' upon form submission.
It works great for me, if 3 forms were filled in and 3 lines were added to the sheet before another came back to edit his responses, the script knows what row to find the data on in the sheet, thus creating the correct template.
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 have tried and combined a few pieces of script to delete rows, but this does not reset the counter. Help resetting responses would be appreciated.
My copy sheet function, and delete all rows function works, but the counter remains, showing 58 responses.
I use the triggers to set the copy and delete functions to occur daily. (sheet url excluding the "docs.google.com..." 0AvTM4SfinH2NdGp1MHdzWms2QnpUMnFiMHJXd1dlV1E&usp) This is what I have so far:
function CopySheet() {
var sh = SpreadsheetApp.getActiveSpreadsheet();
var ss = sh.getSheets()[0];// here I chose to always get the first sheet in the spreadsheet
var inputRange = ss.getRange(1,1,ss.getLastRow(),7);
var data = inputRange.getValues();
var newData = [];
newData.push(['Timestamp','Full Name?','Email?','RAG']);
for(var n=1;n<data.length;++n){ // skip headers by starting at 1
for(var c=0;c<7;c=c+3){
var row = [];
if(c==0){row.push(data[n][0]) ; c++}else{row.push('')};
row.push(data[n][c])
row.push(data[n][c+1]);
row.push(data[n][c+1+1]);//Keep adding a new row and +1 for each extra column
newData.push(row);
}
}
//This next bit creates a copy of the sheet. I would rather a spreadsheet copy but could only get document copy to work
sh.insertSheet().getRange(1,1,newData.length,newData[0].length).setValues(newData);
var doc = DocumentApp.create('Responses document'); // create document
var file = DocsList.getFileById(doc.getId());
file.removeFromFolder(DocsList.getRootFolder());
file.addToFolder(DocsList.getFolder("Folder 1"));
var table = doc.getBody().appendTable(newData); // create table in a separate process so I can set the style below
var style = {};
style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER; // this one has no effect
style[DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily.ARIAL;
style[DocumentApp.Attribute.FONT_SIZE] = 10;
style[DocumentApp.Attribute.FOREGROUND_COLOR] = '#0000ff';
style[DocumentApp.Attribute.BORDER_COLOR] = '#dddddd' ;
table.setAttributes(style);
}
//This section deletes the sheet, leaving the headers; "function deleteAllResponses()" at the bottom should reset counter but does not work
function DeleteSheet() {
SpreadsheetApp.flush();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var datarange = sheet.getDataRange();
var lastrow = datarange.getLastRow();
var values = datarange.getValues();// get all data in a 2D array
for (i=lastrow;i>=2;i--) {
var tempdate = values[i-1][2]; // arrays are 0 indexed so row1 = values[0] and col3 = [2], If I add more columns I need to up this number
{
sheet.deleteRow(i);
function deleteAllResponses() {}
}
}
}
If you mean the counter responses shown on the form:
One option may be to use deleteAllResponses() (read carefully the documentation) from Class Form.
A minimal implementation can be:
/* CODE FOR DEMONSTRATION PURPOSES */
function deleteAllResponses() {
var form, urlForm = SpreadsheetApp.getActiveSpreadsheet().getFormUrl();
if (urlForm) {
form = FormApp.openByUrl(urlForm);
if (form) form.deleteAllResponses();
}
}
#user2847142, #brian-tompsett, #wchiquito
New Google Forms allows you to delete even individual responses from within a Google Form itself without the need of a script.
There is now a simpler method than the answer given by #wchiquito.
--This is now possible on the New Google Forms--
Google announcement on the 10th of February 2016. (googleappsupdates.blogspot.com/2016/02/new-google-forms-now-default-option.html)
How to delete ALL of the responses:
How to delete individual responses:
To delete individual responses you click on the "Responses" tab and choose "Individual". You locate the record you wish to delete and click on the trash can icon to delete that individual response.
This will also reset the counter.
However. The response/s will NOT be deleted from the connected to the form spreadsheet. You will have to manually delete those ones (or using a script).