简体   繁体   English

如何从 xamarin.forms 中的 Google 电子表格读取数据

[英]How to read data from Google spreadsheet in xamarin.forms

I am searching for a way to read/write data from Google Sheets directly.我正在寻找一种直接从Google Sheets读取/写入数据的方法。 Does anyone have an idea how to do that in Xamarin.Forms ?有没有人知道如何在Xamarin.Forms 中做到这一点

Keep in your mind access Google sheets from Windows Form working fine with me using Install-Package Google.Apis.Sheets.v4 Package.请记住,使用Install-Package Google.Apis.Sheets.v4 Package 从 Windows Form 访问Google sheets表格工作正常。

I Used the following link:我使用了以下链接:

https://developers.google.com/sheets/api/quickstart/dotnet https://developers.google.com/sheets/api/quickstart/dotnet

Reading & writing data on spread sheet is not an easy thing.在电子表格上读写数据并不是一件容易的事情。 I would suggest you go with WebView that might solve you issues.我建议您使用可能会解决您问题的WebView Here you might found some clue to do so在这里你可能会找到一些线索来这样做

https://www.twilio.com/blog/2017/03/google-spreadsheets-and-net-core.html https://www.twilio.com/blog/2017/03/google-spreadsheets-and-net-core.html

& here on Google.Apis.Sheets.v4 API's are &这里在 Google.Apis.Sheets.v4 API 是

https://github.com/xamarin/google-apis https://github.com/xamarin/google-apis

& Spreadsheets with C#与 C# 的电子表格

Accessing Google Spreadsheets with C# using Google Data API 使用 Google Data API 使用 C# 访问 Google 电子表格

I created the following Solution and it is working fine for me:我创建了以下解决方案,它对我来说很好用:

You Need to use JSON result from Google sheet, try to use the following steps:您需要使用来自 Google 表格的JSON结果,请尝试使用以下步骤:

1-Publish a google sheet to get an API link that returns JSON , like the following Sheet: 1-发布一个谷歌表以获取返回JSONAPI链接,如下表所示:

https://spreadsheets.google.com/feeds/list/1opP1t_E9xfuLXBkhuyzo5j9k_xBNDx0XKb31JwLP1MM/1/public/values?alt=json https://spreadsheets.google.com/feeds/list/1opP1t_E9xfuLXBkhuyzo5j9k_xBNDx0XKb31JwLP1MM/1/public/values?alt=json

2-You need to generate C# Classes from JSON , maybe you are able to use http://json2csharp.com To get the C# Classes and the RootObject . 2-您需要从JSON生成C#类,也许您可​​以使用http://json2csharp.com来获取C#类和RootObject

3- Add The following Code: 3- 添加以下代码:

   HttpClient client = new HttpClient();
   string URL = "https://spreadsheets.google.com/feeds/list/1opP1t_E9xfuLXBkhuyzo5j9k_xBNDx0XKb31JwLP1MM/1/public/values?alt=json";
   var response = await client.GetAsync(string.Format(url));
   string result = await response.Content.ReadAsStringAsync();
   RootObject root = JsonConvert.DeserializeObject<RootObject>(result);

From the root object, you will be able to access any cell value on the Google sheet.root对象,您将能够访问 Google 工作表上的任何单元格值。

Despite the fact that it really isn't a good idea to use Google Sheets as your online database, there are many better alternatives, if you want to access it from a Xamarin Forms app you can do it using the Sheets API尽管使用 Google Sheets 作为在线数据库确实不是一个好主意,但还有很多更好的选择,如果你想从 Xamarin Forms 应用程序访问它,你可以使用 Sheets API 来完成

Sheets API documentation here Sheets API 文档在这里

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

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