简体   繁体   English

将超过 1000 行写入 Google 表格

[英]Writing more than 1000 rows into Google Sheet

I have to write more than 1000 rows into a Google sheet, therefore I need to increase the number of rows (I got the Google.Apis.Requests.RequestError when I attempted to write 4640 rows).我必须在 Google 工作表中写入 1000 多行,因此我需要增加行数(当我尝试写入 4640 行时,我得到了Google.Apis.Requests.RequestError )。

Searching the Stack Overflow website resulted in information telling me that I needed to use UpdateSheetPropertiesRequest or InsertDimensionRequest and create a new request that could be in the same Spreadsheets.BatchUpdate call.搜索 Stack Overflow 网站后,得到的信息告诉我,我需要使用UpdateSheetPropertiesRequestInsertDimensionRequest并创建一个可能在同一个Spreadsheets.BatchUpdate调用中的新请求。

In line with a Python example on Stack Overflow, I set the properties of the InsertDimensionRequest instance and included them in my existing code (= publicly available C# code by Ian Preston).根据 Stack Overflow 上的 Python 示例,我设置了InsertDimensionRequest实例的属性并将它们包含在我现有的代码中(= Ian Preston 公开的 C# 代码)。

The code then looks like this:然后代码如下所示:

public void AddCells(GoogleSheetParameters googleSheetParameters, List<GoogleSheetRow> rows)
        {
            var requests = new BatchUpdateSpreadsheetRequest { Requests = new List<Request>() }; //Existing code

            int sheetId = GetSheetId(_sheetsService, _spreadsheetId, googleSheetParameters.SheetName); //Existing code

            InsertDimensionRequest insertDimensionRequest = new InsertDimensionRequest(); //Added code
            
            insertDimensionRequest.Range.SheetId = sheetId; //Added code
            insertDimensionRequest.Range.Dimension = "ROWS"; //Added code
            insertDimensionRequest.Range.StartIndex = 999; //Added code
            insertDimensionRequest.Range.EndIndex = 6999; //Added code
            insertDimensionRequest.InheritFromBefore = false; //Added code
            
            var request = new Request { UpdateCells = new UpdateCellsRequest { Start = gc, Fields = "*" } };  //Existing code
            //some code here

            var request1 = new Request { ???????? }; //code to be added - how should the request look like?

            requests.Requests.Add(request);  //Existing code
            requests.Requests.Add(request1); //Added code
         
        }

But I do not know how a new request can be created in C#.但我不知道如何在 C# 中创建新请求。

My question is: How to create such an request (named as request1 in my code)?我的问题是:如何创建这样的请求(在我的代码中命名为request1 )?

Meanwhile, I have managed to figure out an answer.与此同时,我已经设法找出答案。

DimensionRange dr = new DimensionRange
            {
                SheetId = sheetId,
                Dimension = "ROWS",
                StartIndex = 999,
                EndIndex = 6999 // adding extra 6000 rows
            };

var request1 = new Request { InsertDimension = new InsertDimensionRequest { Range = dr, InheritFromBefore = false } };    

And then (the order of the requests matters):然后(请求的顺序很重要):

requests.Requests.Add(request1);
requests.Requests.Add(request);

EDIT 01-06-2022: The entire code using the aforementioned snippet can be downloaded from GitHub - see the GoogleSheets project there.编辑 01-06-2022:使用上述代码段的整个代码可以从GitHub下载 - 请参阅那里的 GoogleSheets 项目。


It is also possible to use userEnteredValue .也可以使用userEnteredValue This clears the sheet and, for me inexplicably, sets the number of rows to an unknow value, but my 4640 rows were accepted.这会清除工作表,并且对我来说莫名其妙地将行数设置为未知值,但我的 4640 行被接受了。

EDIT 31-05-2022: If you know more about the usage of userEnteredValue , feel free to elaborate on this matter.编辑 31-05-2022:如果您对userEnteredValue的用法有更多了解,请随时详细说明此事。

GridRange dr2 = new GridRange
            {
                SheetId = sheetId
            };

var request2 = new Request { UpdateCells = new UpdateCellsRequest { Range = dr2, Fields = "userEnteredValue" } };

And then (the order of the requests probably also matters):然后(请求的顺序可能也很重要):

requests.Requests.Add(request2);
requests.Requests.Add(request);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM