Last active
May 17, 2018 08:18
-
-
Save dmrty/1a4a1c513d19c27e20ffff36f84fa912 to your computer and use it in GitHub Desktop.
Ixd18 "Edit talk in page" script
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
/* | |
* Ixd18 "Edit talk in page" script. This script was hosted by Google script, to deal with requests done from | |
* a speaker Edit page (in a static website) to allow them to modify their content before publication: | |
* https://github.com/IxDA/interaction18/blob/master/themes/interaction18/layouts/edit-session/single.html | |
**/ | |
function doGet(e) { | |
var mode=e.parameters['mode'], | |
slug=e.parameters['slug'], | |
target_field=e.parameters['field'], | |
pwd=e.parameters['pwd'], | |
new_value=e.parameters['value']; | |
if (slug == null || target_field == null || pwd == null ) { | |
return ContentService.createTextOutput("Not found"); | |
} | |
if (target_field == 'PWD' ) { | |
return ContentService.createTextOutput("Can't request password"); | |
} | |
var return_value; | |
if (mode == 'check_password') { | |
return_value=check_password(slug,pwd)?"OK":"Password do not match"; | |
} else { | |
return_value=view(slug,target_field,pwd); | |
} | |
return ContentService.createTextOutput(return_value); | |
} | |
/* | |
* Do Post | |
**/ | |
function doPost(e) { | |
var mode=e.parameters['mode'], | |
slug=e.parameters['slug'], | |
target_field=e.parameters['field'], | |
pwd=e.parameters['pwd'], | |
new_value=e.parameters['value']; | |
if (slug == null || target_field == null || pwd == null || new_value == null ) { | |
return ContentService.createTextOutput("Not found"); | |
} | |
if (! check_password(slug,pwd)) { | |
return ContentService.createTextOutput("Password do not match"); | |
} | |
if (target_field == 'pwd' ) { | |
return ContentService.createTextOutput("Can't request password"); | |
} | |
var result = edit(slug,target_field,new_value,pwd); | |
if (result) { | |
return ContentService.createTextOutput("OK"); | |
} else { | |
return ContentService.createTextOutput("KO"); | |
} | |
} | |
/* | |
* VIEW a field | |
**/ | |
function view(slug,target_field) { | |
var location = { | |
"row":-1, | |
"column":-1, | |
} | |
var column = -1; | |
var ss = SpreadsheetApp.openById("12JUQA4ogk1JGMlRtofFKJmn6NgCGMCCmtOqwqvChxT4"); | |
var sheet = ss.getSheetByName("program"); | |
var keys = Names=sheet.getRange("A:A").getValues(); | |
for (var row = 0; row < keys.length; row++) { | |
if (keys[row][0] == slug) { | |
location.row=row; | |
} | |
} | |
var field_names = sheet.getRange("A1:AZ1").getValues(); | |
for (var column = 0; column < field_names[0].length; column++) { | |
if (field_names[0][column] == target_field) { | |
location.column=column; | |
} | |
} | |
if (location.row == -1 ){ | |
Logger.log("Key not found"); | |
} else if (location.column==-1) { | |
Logger.log("Field not found"); | |
} else { | |
Logger.log("Result is:" + sheet.getRange(location.row+1,location.column+1).getValue()) | |
return sheet.getRange(location.row+1,location.column+1).getValue(); | |
} | |
} | |
/* | |
* EDIT a field | |
**/ | |
function edit(slug,target_field,new_value) { | |
var columns={}; | |
var location = { | |
"row":-1, | |
"column":-1, | |
} | |
var column = -1; | |
var ss = SpreadsheetApp.openById("12JUQA4ogk1JGMlRtofFKJmn6NgCGMCCmtOqwqvChxT4"); | |
var sheet = ss.getSheetByName("program"); | |
var keys = Names=sheet.getRange("A:A").getValues(); | |
for (var row = 0; row < keys.length; row++) { | |
if (keys[row][0] == slug) { | |
location.row=row; | |
} | |
} | |
var field_names = sheet.getRange("A1:AZ1").getValues(); | |
for (var column = 0; column < field_names[0].length; column++) { | |
if (field_names[0][column] == target_field) { | |
location.column=column; | |
} | |
} | |
if (location.row == -1 ){ | |
Logger.log("Key not found"); | |
} else if (location.column==-1) { | |
Logger.log("Field not found"); | |
} else { | |
sheet.getRange(location.row+1,location.column+1).setValue(new_value); | |
return "value_updated"; | |
} | |
return; | |
} | |
function test_check_password() { | |
if (!check_password('test','ABC')) { | |
Logger.log("Test OK"); | |
} | |
if (check_password('test','D2Q30L')) { | |
Logger.log("Test OK"); | |
} | |
if (!check_password('test','')) { | |
Logger.log("Test OK"); | |
} | |
if (!check_password('test',null)) { | |
Logger.log("Test OK"); | |
} | |
} | |
/* | |
* Do Post | |
**/ | |
function check_password(slug,pwd) { | |
var MASTER_PASSWORD="XXXXXX"; | |
if ( MASTER_PASSWORD == pwd) {return true;} | |
Logger.log("Slug="+slug+" PWD="+pwd); | |
var correct_pwd=view(slug,'PWD'); | |
return (correct_pwd != null && correct_pwd == pwd); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment