Skip to content

Commit

Permalink
Merge pull request getodk#12 from nafundi/feature/google-sheets-bad-c…
Browse files Browse the repository at this point in the history
…hars

Feature/google sheets bad chars
  • Loading branch information
mitchellsundt committed Apr 23, 2016
2 parents f409e0a + 0b06769 commit ed43b9f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ private boolean uploadOneSubmission(String id, String instanceFilePath, String j
return false;
}

// make sure column names are legal
for (String n : columnNames) {
if (!isValidGoogleSheetsString(n)) {
mResults.put(id, Collect.getInstance().getString(R.string.google_sheets_invalid_column_form, n));
return false;
}
}

// parses the instance file and populates the answers and photos
// hashmaps.
try {
Expand Down Expand Up @@ -236,6 +244,14 @@ private boolean uploadOneSubmission(String id, String instanceFilePath, String j
e3.printStackTrace();
}

// make sure column names in submission are legal (may be different than form)
for (String n : answersToUpload.keySet()) {
if (!isValidGoogleSheetsString(n)) {
mResults.put(id, Collect.getInstance().getString(R.string.google_sheets_invalid_column_instance, n));
return false;
}
}

// if we have any photos to upload,
// get the picasa album or create a new one
// then upload the photos
Expand Down Expand Up @@ -616,7 +632,7 @@ private boolean uploadOneSubmission(String id, String instanceFilePath, String j
mResults.put(
id,
form_fail
+ "Access denied. Please make sure the spreadsheet owner has granted you access permission.");
+ Collect.getInstance().getString(R.string.google_sheets_access_denied));
} else {
mResults.put(id, form_fail + Html.fromHtml(e.getResponseBody()));
}
Expand Down Expand Up @@ -959,4 +975,16 @@ protected void onProgressUpdate(Integer... values) {
}
}

/**
* Google sheets currently only allows a-zA-Z0-9 and dash
* @param name
* @return
*/
private boolean isValidGoogleSheetsString (String name) {
Pattern p = Pattern
.compile("^[a-zA-Z0-9\\-]+$");
Matcher m = p.matcher(name);
return m.matches();
}

}
11 changes: 6 additions & 5 deletions collect_app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,10 @@
<string name="google_sheets_access_denied">Access denied. Please make sure the spreadsheet owner has granted you access permission.</string>
<string name="google_sheets_update_error">Error updating spreadsheet. Please make sure the spreadsheet owner has granted you write access permission.</string>
<string name="google_sheets_missing_columns">Spreadsheet is missing the following columns: %1$s</string>
<string name="url_error_whitespace">whitespace is not allowed in urls</string>
<string name="password_error_whitespace">leading or trailing whitespace not allowed in passwords</string>
<string name="username_error_whitespace">leading or trailing whitespace not allowed in usernames</string>
<string name="google_play_services_error_occured">Unable to access Google Maps. Is Google Play Services installed?
</string>
<string name="url_error_whitespace">whitespace is not allowed in urls</string>
<string name="password_error_whitespace">leading or trailing whitespace not allowed in passwords</string>
<string name="username_error_whitespace">leading or trailing whitespace not allowed in usernames</string>
<string name="google_play_services_error_occured">Unable to access Google Maps. Is Google Play Services installed?</string>
<string name="google_sheets_invalid_column_form">\'n%1$s\' is an invalid column name in your form. Google Sheets only allows alphanumeric characters and hyphens in column names</string>
<string name="google_sheets_invalid_column_instance">\'n%1$s\' is an invalid column name in your saved instance. Google Sheets only allows alphanumeric characters and hyphens in column names</string>
</resources>

0 comments on commit ed43b9f

Please sign in to comment.