Created
October 29, 2013 19:23
-
-
Save zgohr/7220933 to your computer and use it in GitHub Desktop.
Google Apps script for adding a menu and action to a Google Spreadsheet attached to a Google Form. When used will generate edit links in the first empty column. Provides for a nice GUI for editing form values, especially helpful for forms with drop down selections where editing in the spreadsheet itself is unsatisfactory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onOpen() { | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var menuEntries = [ {name: "Generate Edit Links", functionName: "genEditLinks"}]; | |
ss.addMenu("Generate Edit Links", menuEntries); | |
} | |
function genEditLinks() { | |
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var urlSplit = spreadsheet.getFormUrl().split('/'); | |
var formId = urlSplit[urlSplit.length - 2]; | |
var form = FormApp.openById(formId); | |
var sheet = spreadsheet.getActiveSheet(); | |
var data = sheet.getDataRange().getValues(); | |
var urlCol = sheet.getLastColumn(); | |
if (sheet.getRange(1, urlCol).getValue() != '') { | |
urlCol++; | |
} | |
var responses = form.getResponses(); | |
var timestamps = [], urls = [], resultUrls = [], hyperlink; | |
for (var i = 0; i < responses.length; i++) { | |
hyperlink = '=hyperlink("' + responses[i].getEditResponseUrl() + '", "Edit Form")' | |
timestamps.push(responses[i].getTimestamp().setMilliseconds(0)); | |
urls.push(hyperlink); | |
} | |
for (var j = 1; j < data.length; j++) { | |
// If the timestamps from the grid data match the timestamp of the response | |
// otherwise, this row has been deleted and we don't need its URL | |
resultUrls.push([data[j][0] ? urls[timestamps.indexOf(data[j][0].setMilliseconds(0))] : '']); | |
} | |
sheet.getRange(2, urlCol, resultUrls.length).setValues(resultUrls); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment