Skip to content

Instantly share code, notes, and snippets.

@franklinbaldo
Last active November 26, 2017 15:57
Show Gist options
  • Save franklinbaldo/234cf159fbb64fc1b8f35b83fa9cd2d7 to your computer and use it in GitHub Desktop.
Save franklinbaldo/234cf159fbb64fc1b8f35b83fa9cd2d7 to your computer and use it in GitHub Desktop.
GetEditUrlResponses
function setup(form1ID) {
var scriptProperties = PropertiesService.getScriptProperties();
var form3ID = scriptProperties.getProperty("form3ID");
if (form3ID == null) {
var form3 = FormApp.create('Form3 - Colects Edit URL from form ' + form1ID);
var itemTimestamp = form3.addTextItem()
.setTitle("Timestamp");
var itemEditUrlLong = form3.addTextItem()
.setTitle("EditUrlLong");
var itemEditUrlShort = form3.addTextItem()
.setTitle("EditUrlShort");
var form3ID = form3.getId();
scriptProperties.setProperties({
'form3ID': form3ID
});
};
var form3 = FormApp.openById(form3ID);
return form3ID;
};
function onFistTime() {
var form1ID = "1BIV6TMm3ST5ucG-Ed0p5ovmzgNmfTrsDMNwnIMS18P0";
//var form3ID = "1HSKiCI5dbQhSCHGbsWO3diUqRM0nxJOUB2rcOX-8ztA";
var form3ID = setup(form1ID);
var form3 = FormApp.openById(form3ID);
// This function gets the timestamp and the edit ID from the form1 last response and sends it as a response to the form2
var form1 = FormApp.openById(form1ID);
var form1timezone = SpreadsheetApp.openById(form1.getDestinationId()).getSpreadsheetTimeZone();
var form3Responses = form3.getResponses();
// Let's get the last response from form 1
var form1Responses = form1.getResponses();
var form3itens = form3.getItems();
for (var i = 0; i < form1Responses.length; i++) {
var form1Response = form1Responses[i];
var timestamp = Utilities.formatDate(form1Response.getTimestamp(), form1timezone, "yyyy.MM.dd HH:mm:ss,SSS");
var editUrl = shortenUrl(form1Response.getEditResponseUrl());
var timestampsInForm3 = []
for (var j = 0; j < form3Responses.length; j++) {
var form3Response = form3Responses[i];
var form3ItemResponses = form3Response.getItemResponses();
var form3TimestampResponse = form3ItemResponses[0].getResponse();
timestampsInForm3.push(form3TimestampResponse)
;}
if (timestampsInForm3.indexOf(timestamp)<0) {
var responseTimestamp = form3itens[0].asTextItem().createResponse(timestamp);
var responseLongUrl = form3itens[1].asTextItem().createResponse(editUrl.longUrl);
var responseShortUrl = form3itens[2].asTextItem().createResponse(editUrl.id);
var response = form3.createResponse()
.withItemResponse(responseTimestamp)
.withItemResponse(responseLongUrl)
.withItemResponse(responseShortUrl)
.submit();
};
};
};
function onSubmit() {
var form1ID = "1BIV6TMm3ST5ucG-Ed0p5ovmzgNmfTrsDMNwnIMS18P0";
var form3ID = setup(form1ID); // * you may preffer to set it youself;
// This function gets the timestamp and the edit ID from the form1 last response and sends it as a response to the form2
var form1 = FormApp.openById(form1ID);
var form1timezone = SpreadsheetApp.openById(form1.getDestinationId()).getSpreadsheetTimeZone();
// getting the lastresponse from form 1
var form1Responses = form1.getResponses();
var form1lastesponse = form1Responses[form1Responses.length-1];
// getting the timestamp from the last response
var timestamp = Utilities.formatDate(form1lastesponse.getTimestamp(), form1timezone, "yyyy.MM.dd HH:mm:ss,SSS");
// getting the editUrl from the last response
// **You need to allow the use o UrlShortner on menu Resources \ Advanced Google Services
var editUrl = UrlShortener.Url.insert({longUrl: form1lastesponse.getEditResponseUrl()});
// making the responses
var form3 = FormApp.openById(form3ID);
var form3itens = form3.getItems();
var responseTimestamp = form3itens[0].asTextItem().createResponse(timestamp);
var responseLongUrl = form3itens[1].asTextItem().createResponse(editUrl.longUrl);
var responseShortUrl = form3itens[2].asTextItem().createResponse(editUrl.id);
// submiting the responses
var response = form3.createResponse()
.withItemResponse(responseTimestamp)
.withItemResponse(responseLongUrl)
.withItemResponse(responseShortUrl)
.submit();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment