0

我試圖與Google Spreadsheet API一起玩,我只是與我的應用程序合併。可以說我有這個工作表Google工作表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; 
} 

非常感謝!

回答

1

我會將數據移動到2張/選項卡。由於您很可能會有兩個具有相同名稱的人,請爲每個數據源分配一個唯一的ID,這兩個數據源之間是一致的。然後使用this tutorial中的部分代碼將數據讀入對象,然後將其解析爲第三張表。要創建的對象,複製與開始的完整代碼下的部分:

////////////////////////////////////////////////////////////////////////////////////////// 
// 
// 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()finction,從網站複製上面鏈接:

////////////////////////////////////////////////////////////////////////////////////////// 
// 
// 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'; 
} 
+0

感謝@Karl_S這是真棒! – Jagmaster

相關問題