【发布时间】:2017-07-14 16:13:00
【问题描述】:
我无法将这些嵌套列表包裹起来。
为了让 Google Sheets API 插入数据,我必须根据我的理解传递一个列表列表,它代表行和列。
我怎么不知道如何加载列表中的值。
Dim requestBody As New ValueRange()
Dim rowCount As Integer = 2
requestBody.Values = New List(Of IList(Of Object))() From {New List(Of Object)()}
requestBody.Values(0).Add(1)
requestBody.Values(0).Add(2)
requestBody.Values(0).Add(3)
requestBody.Values(0).Add(4)
requestBody.Values(0).Add(5)
requestBody.Values(1).Add("a")
requestBody.Values(1).Add("b")
requestBody.Values(1).Add("c")
requestBody.Values(1).Add("d")
requestBody.Values(1).Add("e")
当我到达第二个值(行)时,我收到一个错误:索引超出范围。
System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.'
感谢您的帮助。
PS 如果有人可以分享将 DataTable 转换为该对象(列表列表)的示例,那么循环遍历行并手动构建该对象会容易得多,并且将永远感激不尽。
https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append
更新:这是我正在尝试创建的函数的完整代码。
Imports System.IO
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Services
Imports Google.Apis.Sheets.v4
Imports Google.Apis.Sheets.v4.Data
Namespace Settings
Public Class SettingsPage
Inherits System.Web.UI.Page
Dim _connstring As String
Dim googleSecretJsonFilePath = Server.MapPath("GoogleSecret\GoogleSecret.json")
Dim applicationName = "OPT Web Services"
Dim scopes As String() = {SheetsService.Scope.Spreadsheets, SheetsService.Scope.Drive, SheetsService.Scope.DriveFile}
Dim googleService = New GoogleService(googleSecretJsonFilePath, applicationName, scopes)
Dim spreadSheetId = "14N-1R##########################sCWA7U" 'Private Live Sheet
Protected Sub btnSendVersions_Click(sender As Object, e As EventArgs) Handles btnSendVersions.Click
'TODO: Append KS_SedonaSync_Version to Google Docs - Tab
'TODO: Append SS_Version to Google Docs
Dim range = "'Customer OPT Versions'!A:E"
'A (0) - OPT_Customer_Number
'B (1) - Version_Id
'C (2) - Date_Installed
'D (3) - SedonaSync_Event_Id
'E (4) - Version
Dim ds As DataSet = Master.OPTWebConfig.GetVersionList(_connstring)
Dim sheetService = googleService.GetSheetsService()
Dim valueInputOption As SpreadsheetsResource.ValuesResource.AppendRequest.ValueInputOptionEnum = SpreadsheetsResource.ValuesResource.AppendRequest.ValueInputOptionEnum.RAW
Dim insertDataOption As SpreadsheetsResource.ValuesResource.AppendRequest.InsertDataOptionEnum = SpreadsheetsResource.ValuesResource.AppendRequest.InsertDataOptionEnum.INSERTROWS
Dim requestBody As New ValueRange()
'Update OPT Version Numbers
Dim rowCount As Integer = ds.Tables("Versions").Rows.Count
'TODO: Make this work: requestBody.Values = ds.Tables("Versions").Rows
'Test Adding Multiple Rows, May need to loop through DataTable rows if we cannot find a way to convert the DataTable.
requestBody.Values = New List(Of IList(Of Object))() From {New List(Of Object)()}
requestBody.Values(0).Add(1)
requestBody.Values(0).Add(2)
requestBody.Values(0).Add(3)
requestBody.Values(0).Add(4)
requestBody.Values(0).Add(5)
requestBody.Values(1).Add("a")
requestBody.Values(1).Add("b")
requestBody.Values(1).Add("c")
requestBody.Values(1).Add("d")
requestBody.Values(1).Add("e")
Dim request As SpreadsheetsResource.ValuesResource.AppendRequest = sheetService.Spreadsheets.Values.Append(requestBody, spreadSheetId, range)
request.ValueInputOption = valueInputOption
request.InsertDataOption = insertDataOption
Dim response = request.Execute()
'TODO: Mark Rows in ds.Tables("Versions") as Sent_To_OPT = "Y" if Response is 200:OK
End Sub
End Class
Public Class GoogleService
Private ReadOnly _googleSecretJsonFilePath As String
Private ReadOnly _applicationName As String
Private ReadOnly _scopes As String()
Public Sub New(googleSecretJsonFilePath As String, applicationName As String, scopes As String())
_googleSecretJsonFilePath = googleSecretJsonFilePath
_applicationName = applicationName
_scopes = scopes
End Sub
Public Function GetGoogleCredential() As GoogleCredential
Dim credential As GoogleCredential
Using stream = New FileStream(_googleSecretJsonFilePath, FileMode.Open, FileAccess.Read)
credential = GoogleCredential.FromStream(stream).CreateScoped(_scopes)
End Using
Return credential
End Function
Public Function GetSheetsService() As SheetsService
Dim credential = GetGoogleCredential()
Dim Base As New BaseClientService.Initializer
Base.HttpClientInitializer = credential
Base.ApplicationName = _applicationName
Dim sheetsService = New SheetsService(Base)
Return sheetsService
End Function
End Class
End Namespace
【问题讨论】:
标签: .net vb.net google-api google-api-dotnet-client google-sheets-api