Skip to content

Instantly share code, notes, and snippets.

@kylekyle
Last active September 6, 2020 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylekyle/9ebc1e2887579cc5f92402b1969a37d3 to your computer and use it in GitHub Desktop.
Save kylekyle/9ebc1e2887579cc5f92402b1969a37d3 to your computer and use it in GitHub Desktop.
Use Google Forms as an Electronic Medical Record system :-)
{
"timeZone": "America/New_York",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"oauthScopes": [
"https://www.googleapis.com/auth/forms",
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/drive.readonly",
"https://www.googleapis.com/auth/script.container.ui",
"https://www.googleapis.com/auth/script.external_request"
]
}
<style>
* {
font-family: Roboto;
}
li {
margin: 5px 0;
}
</style>
<script>
function changeForm(id) {
google.script.run.changeForm(id);
google.script.host.close();
}
</script>
<ul>
/**
* @NotOnlyCurrentDoc
*
* To install, you will need to register open and submit as triggers at
* https://script.google.com/
*/
const EMR = eval(UrlFetchApp.fetch('http://bit.ly/meganemr').getContentText());
function open(e) { EMR.open(e); };
function submit(e) { EMR.submit(e); };
function choose(...params) { EMR.choose(...params); }
const properties = PropertiesService.getScriptProperties();
const ENROLLMENT_FORM_ID = "138pqAeU4nWaDkALfTclsdbW-jyrgwW3mC1Ta1zvsnPQ";
const findFormsQuery = Utilities.formatString("title contains 'EMR Form' and mimeType='%s'", MimeType.GOOGLE_FORMS);
(function() {
return {
open: e => {
Logger.log('EMR: Opened ' + FormApp.getActiveForm().getTitle());
const responseID = PropertiesService.getDocumentProperties().getProperty("EMR_REGISTRATION_ID");
if (e.source.getId() != ENROLLMENT_FORM_ID) {
const files = DriveApp.searchFiles(findFormsQuery);
const template = UrlFetchApp.fetch('http://bit.ly/changeformhtml').getContentText();
const html = HtmlService.createHtmlOutput(template).setTitle("Change Form");
while (files.hasNext()) {
const file = files.next();
if (file.getId() != ENROLLMENT_FORM_ID) {
const report = FormApp.openById(file.getId());
const reportLink = Utilities.formatString("<li><a href='javascript:changeForm(\"%s\")'>%s</a>", report.getId(), report.getName());
html.append(reportLink);
}
}
FormApp.getUi().showSidebar(html);
}
},
choose: id => {
const target = FormApp.getActiveForm();
target.getItems().forEach(item => form.deleteItem(item));
const source = FormApp.openById(id);
target.setTitle(source.getTitle());
target.setDescription(source.getDescription());
source.getItems().forEach(item => item.duplicate());
},
submit: e => {
const copyPatientInfo = (patientInfo, form) => {
form.setDescription("This is a patient file. To add information to it, use the 'Change Form' menu on the right.")
form.getItems().forEach(item => form.deleteItem(item));
patientInfo.getItemResponses().forEach(response => {
const formItem = form.addSectionHeaderItem();
formItem.setTitle(response.getItem().getTitle());
formItem.setHelpText([response.getResponse()].join("\n"));
});
};
// A new patient just enrolled, create their file
if (e.source.getId() == ENROLLMENT_FORM_ID) {
const nameItem = e.response.getItemResponses().find(itemResponse =>
itemResponse.getItem().getTitle() == 'Name'
);
const name = nameItem.getResponse();
const patientFile = DriveApp.getFileById(e.source.getId()).makeCopy();
patientFile.setName(name);
// we save the enrollment response id in the script properties
// the key is their patient file id
properties.setProperty(patientFile.getId(), e.response.getId());
const patientForm = FormApp.openById(patientFile.getId());
patientForm.setTitle(name);
copyPatientInfo(e.response, patientForm);
} else {
const name = DriveApp.getFileById(e.source.getId()).getName();
Logger.log("EMR: Added %s report to %s's file", e.source.getTitle(), name);
const responseId = properties.getProperty(e.source.getId());
const patientInfo = FormApp.openById(ENROLLMENT_FORM_ID).getResponse(responseId);
copyPatientInfo(patientInfo, e.source);
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment