2012-10-29 44 views
2

如何通過Google Spreadsheet API在程序創建的工作表上添加簡單的標題行?它很難理解他們的doc,它只提到了如何在已經創建的標題行中添加一行。如果基於細胞進料被用於第一次用於添加標題行,在Google Spreadsheet API中添加一個簡單的標題行

// Fetch the cell feed of the worksheet. 
    CellQuery cellQuery = new CellQuery(worksheet.CellFeedLink); 
    CellFeed cellFeed = service.Query(cellQuery); 

    // Iterate through each cell, updating its value if necessary. 
    foreach (CellEntry cell in cellFeed.Entries) 
    { 
    if (cell.Title.Text == "A1") 
    { 
     cell.InputValue = "name"; 
     cell.Update(); 
    } 
    else if (cell.Title.Text == "B1") 
    { 
     cell.InputValue = "age"; 
     cell.Update(); 
    } 
    } 

此代碼不起作用如cellFeed.Entries是0,從而控制不進入過的for循環。 我已經添加了(4)行和(10)列的工作表。但是,如何首先輸入簡單的標題行(作爲列),然後再輸入其他行(作爲這些列中的值)? 我知道已經很少有人問過問題,但是他們中沒有人有答案,這很令人沮喪,很難理解這個API如此複雜的實現是什麼?

回答

5

的答案是:

 CellQuery cellQuery = new CellQuery(worksheet.CellFeedLink); 
     CellFeed cellFeed = _SpService.Query(cellQuery); 

     CellEntry cellEntry = new CellEntry(1, 1, "name"); 
     cellFeed.Insert(cellEntry); 
     cellEntry = new CellEntry(1, 2, "age"); 
     cellFeed.Insert(cellEntry); 
     cellEntry = new CellEntry(1, 3, "address"); 
     cellFeed.Insert(cellEntry); 

然後可將行下面添加作爲每匹配上述標題行中的文檔。

+0

你用什麼代碼來創建工作表?我正在創建一個工作表,我可以看到它添加到電子表格中,但我仍然無法獲得單元格鏈接..我甚至試圖從電子表格對象中再次讀取工作表的提要。 – SgtPooki

+0

剛剛得到它。顯然,service.Insert(worksheetFeed,worksheetEntry)返回一個worksheetEntry對象..所以如果你保存它,它基本上就像更新工作表以包含所有的feedlinks/uris/id等。 – SgtPooki

1

默認情況下,單元格查詢不返回空單元格。 您需要覆蓋此設置。

編輯您的代碼如下所示:

CellQuery cellQuery = new CellQuery(worksheet.CellFeedLink); 
cellQuery.ReturnEmpty = ReturnEmptyCells.yes; 
CellFeed cellFeed = _SpService.Query(cellQuery); 
相關問題