使用VB.Net在Google表格中批量更新行

时间:2018-03-11 03:38:39

标签: vb.net google-sheets-api

我无法使用VB.Net更新Google表格中的行。 这是我的代码:

Dim service = New SheetsService(New BaseClientService.Initializer() With {.HttpClientInitializer = credential, .ApplicationName = ApplicationName})
        Dim spreadsheetId As String = "1mW-ndDolz_uECCMmAmKZ-sANoM9H53eqc231g98I06U"
        Dim spreadsheet As Spreadsheet = Nothing
        Dim content As BatchUpdateSpreadsheetRequest = New BatchUpdateSpreadsheetRequest()
        Dim request As Request = New Request()
        Dim deleteDimensionRequest As DeleteDimensionRequest = New DeleteDimensionRequest()
        Dim dimensionRange As DimensionRange = New DimensionRange()

        dimensionRange.Dimension = "A:B"
        dimensionRange.StartIndex = 0
        dimensionRange.EndIndex = 3
        dimensionRange.SheetId = 0
        deleteDimensionRequest.Range = dimensionRange

        Dim requests As IList(Of Object) = New List(Of Object)()
        requests.Add(request)
        content.Requests = requests
        Try
            service.Spreadsheets.BatchUpdate(content, spreadsheetId)
        Catch e As IOException
            MsgBox(e.Message)
        End Try

它显示错误说

  

无法投射类型的对象   'System.Collections.Generic.List 1[System.Object]' to type 'System.Collections.Generic.IList 1 [Google.Apis.Sheets.v4.Data.Request]'。

我在这里做错了什么?

2 个答案:

答案 0 :(得分:0)

我不知道VB.NET是如何工作的,但根据您的示例代码和错误消息,我会假设问题是这一行: Dim requests As IList(Of Object) = New List(Of Object)()

应该是: Dim requests As IList(Of Request) = New List(Of Request)()

答案 1 :(得分:0)

我确定它可以工作,但我不知道它的缺点

     Dim requests As IList(Of Request) 
     dim Req1 as List(Of Request) = new List(Of Request) 
    Req1.Add(request) 
    Requests = Req1.ToList()

 content.Requests = requests