我正在尝试使用我刚刚与我的应用程序合并的Google Spreadsheet API。假设我有这个工作表
https://docs.google.com/spreadsheets/d/1TfRWRh0l09cxZ4HqwYWhRiBK4Lll3Jvj0XHpO-KEK2E/edit#gid=0
我希望将两个数据范围解析为1个输出表:名称,年龄,业余爱好,职业,学校和性别。我如何编写代码来区分数据范围1和2?
以下是代码:
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;
}
非常感谢!
答案 0 :(得分:1)
我会将数据移动到2张/标签。由于您很可能有2个具有相同名称的人员,因此请为每个人分配两个数据源之间一致的唯一ID。然后使用this tutorial中的部分代码将数据读入Objects,然后将其解析为第三张。要创建对象,请复制完整代码下的部分,该部分以:
开头//////////////////////////////////////////////////////////////////////////////////////////
//
// The code below is reused from the 'Reading Spreadsheet data using JavaScript Objects'
// tutorial.
//
//////////////////////////////////////////////////////////////////////////////////////////
从那里你将调用getRowsData()
函数来从两个数据集中的每一个获取数据。然后根据唯一ID合并数据并填充第三张表。然而,问题是你需要一些方法来触发它,你每次都会重写数据。
也就是说,这可以通过第三张表中的3个公式来完成,并且可以保持最新的实时状态。请参阅this copy of your spreadsheet的合并选项卡/表格上的单元格A1,E1和E2。
以下是您将用于getRowsData()完成的完整代码,从上面链接的网站复制:
//////////////////////////////////////////////////////////////////////////////////////////
//
// 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';
}