Google 工作表 API V4 添加行 .NET

Google Sheets API V4 Add Row .NET

Google 文档页面说

The Sheets API v4 does provide an AppendCells request that can be used with the spreadsheets.batchUpdate method to append a row of data to a sheet (and simultaneously update the cell properties and formatting, if desired).

这确实适用于添加新行,即使创建 RowData 负载很繁琐。但是,这不允许设置 ValueInputOption。

Google也说

However, it is usually easier to simply determine the A1 notation of the row you wish to add, and then issue a spreadsheets.values.update request to overwrite that row. In this case, any data in the specified row is overwritten.

现在这适用于更新现有行上的数据 - 包括 ValueInputOption。但是,当我使用它来附加新行(即提供下一行的范围)时,服务器 returns 出现 503 错误。一定有我遗漏的技巧吗?

我应该按照评论者的建议在我的问题中发布代码。但是,我将其作为此答案的一部分发布 - 这显然是一个次优解决方案,但符合我的目的。

这是我最终得到的代码(省略了 OAuth 和服务实例化)

 Public Function AddRows(Values As Object()()) As Boolean

        Try

            'Create dummy rows value payload
            Dim cell2add As New CellData
            Dim cellval As New ExtendedValue
            cellval.NumberValue = 0
            cell2add.UserEnteredValue = cellval
            Dim data2add As New RowData
            data2add.Values = {cell2add}

            Dim rowdataList As New List(Of RowData)
            For i = 0 To UBound(Values)
                rowdataList.Add(data2add)
            Next

            'Add a request to append to the sheet's data (expand grid)
            Dim appendRequest As New AppendCellsRequest
            appendRequest.SheetId = SheetID
            appendRequest.Rows = rowdataList.ToArray           
            appendRequest.Fields = "*"
            Dim request As New Request
            request.AppendCells = appendRequest

            'Execute the request
            Dim bRequest As New BatchUpdateSpreadsheetRequest
            bRequest.Requests = {request}
            Dim bResponse As BatchUpdateSpreadsheetResponse = Service.Spreadsheets.BatchUpdate(bRequest, DataBaseName).Execute()

            'Now update the newly added rows with data
            Dim index As Integer = GetRowCount() - Values.Length + 2 'GetRowCount() calls the service to get sheet metadata and returns the rows of data (excluding the headers)
            Dim vals As New ValueRange
            vals.Values = Values
            Dim urequest As SpreadsheetsResource.ValuesResource.UpdateRequest =
                Service.Spreadsheets.Values.Update(vals, DataBaseName, Name & "!A" & index)
            urequest.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED
            Dim response As UpdateValuesResponse = urequest.Execute
            Return response.UpdatedRows = Values.Length

        Catch ex As Exception
            Trace.WriteLine(Now.ToLongTimeString & ":" & ex.Message)
            Return False
        End Try

    End Function

简而言之,我正在调用 AppendCells 来扩展 'grid' 的大小,然后更新由此创建的空白行中的值。如果您尝试更新新行的值,服务器 returns 会出现 'Service not available' 错误。我在尝试更新现有行中的值 (values.batchUpdate) 并在同一请求中添加新行时发​​现了这个问题。这种情况下的错误消息谈到 'beyond the grid' 更新。

没有 API 用于通过 spreadsheets.values 集合(支持 A1 表示法和 ValueInputOption 的集合)追加行。我们正在内部将其作为功能请求进行跟踪,并可能会增加支持。同时,附加行的唯一方法是通过 batchUpdate 方法(但是,如果您不需要 CellData 对象提供的额外功能,例如格式化,那么按照您所说的那样做是很乏味的)。