首页 > 解决方案 > Google Sheets API .NET v4 为网格单元创建下拉列表

问题描述

我正在尝试在网格上创建一个下拉列表。我正在遵循此页面上的建议。但是,我无法为UserEnteredValue. 我得到的带下划线的错误:

无法将类型“Google.Apis.Sheets.v4.Data.ConditionValue”隐式转换为“System.Collections.Generic.IList”。存在显式转换(您是否缺少演员表?)

    public Request createDataValidationRequest(int sheetID, int startRow, int endRow, int startColumn, int endColumn)
    {
        var updateCellsRequest = new Request() {
            SetDataValidation = new SetDataValidationRequest()
            {
                Range = new GridRange()
                {
                    SheetId = sheetID,
                    StartRowIndex = startRow,
                    StartColumnIndex = startColumn,
                    EndRowIndex = endRow,
                    EndColumnIndex = endColumn
                },
                Rule = new DataValidationRule()
                {
                    Condition = new BooleanCondition()
                    {
                        Type = "ONE_OF_LIST",
                        Values = new ConditionValue()
                        {
                            UserEnteredValue = "awer"
                        }
                    },
                    InputMessage = "Select an Option",
                    ShowCustomUi = true,
                    Strict = true
                }
            }
        };
        return updateCellsRequest;
    }

标签: c#.netvisual-studiogoogle-sheetsgoogle-sheets-api

解决方案


对于我的情况,我能够获得以下信息。

                        SpreadsheetsResource.GetRequest request = _sheetsService.Spreadsheets.Get(newTimeSheet.Id);
                        Spreadsheet spreadsheet = request.Execute();
                        SheetProperties timeSheetProperties = new SheetProperties();
                        for (int z = 0; z < spreadsheet.Sheets.Count; z++)
                        {
                            SheetProperties sheetProperties = spreadsheet.Sheets[z].Properties;
                            if (sheetProperties.Title == "3c TIME")
                            {
                                timeSheetProperties = sheetProperties;
                                break;
                            }
                        }
                        var updateCellsRequest = new Request()
                        {
                            SetDataValidation = new SetDataValidationRequest()
                            {
                                Range = new GridRange()
                                {
                                    SheetId = timeSheetProperties.SheetId,
                                    StartRowIndex = 2,
                                    StartColumnIndex = 0,
                                    EndColumnIndex = 1
                                },
                                Rule = new DataValidationRule()
                                {
                                    Condition = new BooleanCondition()
                                    {
                                        //Type = "ONE_OF_RANGE",
                                        Type = "ONE_OF_LIST",
                                        Values = new List<ConditionValue>()
                                    {
                                        new ConditionValue()
                                        {
                                            UserEnteredValue = "YES",
                                        },
                                        new ConditionValue()
                                        {
                                            UserEnteredValue = "NO",
                                        },
                                        new ConditionValue()
                                        {
                                            UserEnteredValue = "MAYBE",
                                        }
                                    }
                                    },
                                    InputMessage = "Select an Option",
                                    ShowCustomUi = true,
                                    Strict = true
                                }
                            }
                        };
                        var requestBody = new Google.Apis.Sheets.v4.Data.BatchUpdateSpreadsheetRequest();
                        var requests = new List<Request>();
                        requests.Add(updateCellsRequest);
                        requestBody.Requests = requests;
                        var batchRequest = _sheetsService.Spreadsheets.BatchUpdate(requestBody, newTimeSheet.Id);
                        batchRequest.Execute();

推荐阅读