Reputation: 107
I am trying to play around with Google Spreadsheet API I just incorporated with my App. lets say I have this worksheet
https://docs.google.com/spreadsheets/d/1TfRWRh0l09cxZ4HqwYWhRiBK4Lll3Jvj0XHpO-KEK2E/edit#gid=0
and I want to parse both data ranges into 1 output table of: Name, Age, Hobby, Occupation, School, and Gender. How would I write the code to differentiate between data range 1 and 2?
Here is the code:
private List<String> getDataFromApi() throws IOException {
String spreadsheetId = "1TfRWRh0l09cxZ4HqwYWhRiBK4Lll3Jvj0XHpO-KEK2E";
String range = "test!A3:D6,B10:C13";
List<String> results = new ArrayList<String>();
ValueRange response = this.mService.spreadsheets().values()
.get(spreadsheetId, range)
.execute();
List<List<Object>> values = response.getValues();
if (values != null) {
results.add("Name, Age, Hobby, Occupation, School, Gender");
for (List row : values) {
results.add(row.get(0) + ", " + row.get(1) + ", " + row.get(2) + ", " + row.get(3)); //then i'm stuck//
}
}
return results;
}
Many thanks!
Upvotes: 1
Views: 2617
Reputation: 3554
I would move the data to 2 sheets/tabs. Since you will most likely have 2 people with the same name, assign each a unique ID which is consistent between the two data sources. Then use a portion of the code in this tutorial to read the data into Objects then parse it out to a third sheet. To create the objects, you copy the portion under the Full Code which starts with:
//////////////////////////////////////////////////////////////////////////////////////////
//
// The code below is reused from the 'Reading Spreadsheet data using JavaScript Objects'
// tutorial.
//
//////////////////////////////////////////////////////////////////////////////////////////
From there you will be calling the getRowsData()
function to get the data from each of the two data sets. Then merge the data based on a unique ID and populate the third sheet. The problem with this, however, is you need some way to trigger it and you will be re-writing the data each time.
That said, this can all be done with 3 formulas in the third sheet and will stay up to date real time. See cells A1, E1, and E2 on the Merged tab/sheet of this copy of your spreadsheet.
Here is the complete code you will use for the getRowsData() finction, copied from the site linked above:
//////////////////////////////////////////////////////////////////////////////////////////
//
// The code below is reused from the 'Reading Spreadsheet data using JavaScript Objects'
// tutorial.
//
//////////////////////////////////////////////////////////////////////////////////////////
// getRowsData iterates row by row in the input range and returns an array of objects.
// Each object contains all the data for a given row, indexed by its normalized column name.
// Arguments:
// - sheet: the sheet object that contains the data to be processed
// - range: the exact range of cells where the data is stored
// - columnHeadersRowIndex: specifies the row number where the column names are stored.
// This argument is optional and it defaults to the row immediately above range;
// Returns an Array of objects.
function getRowsData(sheet, range, columnHeadersRowIndex) {
columnHeadersRowIndex = columnHeadersRowIndex || range.getRowIndex() - 1;
var numColumns = range.getEndColumn() - range.getColumn() + 1;
var headersRange = sheet.getRange(columnHeadersRowIndex, range.getColumn(), 1, numColumns);
var headers = headersRange.getValues()[0];
return getObjects(range.getValues(), normalizeHeaders(headers));
}
// For every row of data in data, generates an object that contains the data. Names of
// object fields are defined in keys.
// Arguments:
// - data: JavaScript 2d array
// - keys: Array of Strings that define the property names for the objects to create
function getObjects(data, keys) {
var objects = [];
for (var i = 0; i < data.length; ++i) {
var object = {};
var hasData = false;
for (var j = 0; j < data[i].length; ++j) {
var cellData = data[i][j];
if (isCellEmpty(cellData)) {
continue;
}
object[keys[j]] = cellData;
hasData = true;
}
if (hasData) {
objects.push(object);
}
}
return objects;
}
// Returns an Array of normalized Strings.
// Arguments:
// - headers: Array of Strings to normalize
function normalizeHeaders(headers) {
var keys = [];
for (var i = 0; i < headers.length; ++i) {
var key = normalizeHeader(headers[i]);
if (key.length > 0) {
keys.push(key);
}
}
return keys;
}
// Normalizes a string, by removing all alphanumeric characters and using mixed case
// to separate words. The output will always start with a lower case letter.
// This function is designed to produce JavaScript object property names.
// Arguments:
// - header: string to normalize
// Examples:
// "First Name" -> "firstName"
// "Market Cap (millions) -> "marketCapMillions
// "1 number at the beginning is ignored" -> "numberAtTheBeginningIsIgnored"
function normalizeHeader(header) {
var key = "";
var upperCase = false;
for (var i = 0; i < header.length; ++i) {
var letter = header[i];
if (letter == " " && key.length > 0) {
upperCase = true;
continue;
}
if (!isAlnum(letter)) {
continue;
}
if (key.length == 0 && isDigit(letter)) {
continue; // first character must be a letter
}
if (upperCase) {
upperCase = false;
key += letter.toUpperCase();
} else {
key += letter.toLowerCase();
}
}
return key;
}
// Returns true if the cell where cellData was read from is empty.
// Arguments:
// - cellData: string
function isCellEmpty(cellData) {
return typeof(cellData) == "string" && cellData == "";
}
// Returns true if the character char is alphabetical, false otherwise.
function isAlnum(char) {
return char >= 'A' && char <= 'Z' ||
char >= 'a' && char <= 'z' ||
isDigit(char);
}
// Returns true if the character char is a digit, false otherwise.
function isDigit(char) {
return char >= '0' && char <= '9';
}
Upvotes: 1