我尝试从30个以上的单元中获取数据(如果我没记错的话,大约是40个),但是一直到30个单元以下。如何解决此问题?
行有30个项目,但我选择了更多
namespace CBox
{
public partial class Form1 : Form
{
static string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly };
static string ApplicationName = "CBox";
static String spreadsheetId = "1LovVHkHGQYADfgEBMGEaZFTDkSgQEQhQ-FuRZHWkXHo";
String range;
static String credPath = "token.json";
static FileStream stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read);
SpreadsheetsResource.ValuesResource.GetRequest request;
static UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, new FileDataStore(credPath, true)).Result;
SheetsService service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Items.Clear();
range = "A2:AT9";
request = service.Spreadsheets.Values.Get(spreadsheetId, range);
//request.MajorDimension = SpreadsheetsResource.ValuesResource.GetRequest.MajorDimensionEnum.COLUMNS;
ValueRange response = request.Execute();
IList<IList<Object>> values = response.Values;
switch (comboBox1.SelectedIndex)
{
***********
case 9: //120
foreach (var rows in values)
{
if (rows[28] != "" && rows[29] != "")
{
comboBox2.Items.Add(rows[0]);
}
}
break;
case 10: //130
foreach (var rows in values)
{
if (rows[30] != "" && rows[31] != "")
{
comboBox2.Items.Add(rows[0]);
}
}
break;
}
}
}
}
答案 0 :(得分:0)
您可以检查范围内的所有单元格是否都有值?将返回包含值的单元格的最大范围。因此,如果您在范围末尾输入了一些空值,则不会返回它们。
编辑.. 我试图从您在评论中发布的表格中获取范围,没问题。
{"majorDimension":"ROWS","range":"'????1'!A9:AT9","values":[["Prime Grand 2835 3 Lens","74","15","180","0,72","160","75","Cool White","http://tdkatod.ru/1497","","","","","","100","75","110","80","130","90","140","100","150","115","160","125","170","130","180","140","190","150","200","160","210","170","220","180","230","190","240","220"]],"ETag":null}
您使用哪个API版本?如果您在Google API页面上尝试使用代码(C#),那么它将正常工作。
答案 1 :(得分:0)
Google严格按照内容切割行。
假设您在Google表格中有一个表格:
[dsds] [dwdw] []
[dswg] [] []
[dswf] [] [dwdw]
如果您请求指定范围(A0:C2),然后在调试过程中查看VisualStudio中的值,您会发现每行中没有三个元素,但是恰好需要的数量。不会自动上传任何多余的空单元格。
[dsds] [dwdw]
[dswg]
[dswf] [] [dwdw]
我没有考虑到这一点。我将值与字符串中的索引进行了比较,实际上不存在该索引,尽管从理论上讲该单元格处于请求的范围内。