- Home /
Google Sheets API works in editor but not work in player
I use Google Sheets APIs for my project. It works fine in editor, but not work if I build a windows player.
seems like the problem is 'DateTimeRenderOption' function, but have no idea how to fix it.
I am not sure if I missed something, hope someone could give me an idea, thanks.
This is the exception I got from windows player.
System.ArgumentException: Get Method not found for 'DateTimeRenderOption'
at System.Reflection.MonoProperty.GetValue (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] index, System.Globalization.CultureInfo culture) [0x00000] in <00000000000000000000000000000000>:0
at System.Reflection.PropertyInfo.GetValue (System.Object obj, System.Object[] index) [0x00000] in <00000000000000000000000000000000>:0
at Google.Apis.Requests.Parameters.ParameterUtils.IterateParameters (System.Object request, System.Action`3[T1,T2,T3] action) [0x00000] in <00000000000000000000000000000000>:0
at Google.Apis.Requests.Parameters.ParameterUtils.CreateParameterDictionary (System.Object request) [0x00000] in <00000000000000000000000000000000>:0
at Google.Apis.Requests.ClientServiceRequest`1[TResponse].CreateBuilder () [0x00000] in <00000000000000000000000000000000>:0
at Google.Apis.Requests.ClientServiceRequest`1[TResponse].CreateRequest (System.Nullable`1[T] overrideGZipEnabled) [0x00000] in <00000000000000000000000000000000>:0
at Google.Apis.Requests.ClientServiceRequest`1+<ExecuteUnparsedAsync>d__34[TResponse].MoveNext () [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[TResult].Start[TStateMachine] (TStateMachine& stateMachine) [0x00000] in <00000000000000000000000000000000>:0
at Google.Apis.Requests.ClientServiceRequest`1[TResponse].ExecuteUnparsedAsync (System.Threading.CancellationToken cancellationToken) [0x00000] in <00000000000000000000000000000000>:0
at Google.Apis.Requests.ClientServiceRequest`1[TResponse].Execute () [0x00000] in <00000000000000000000000000000000>:0
at GoogleSheetsLoader.Load (System.String spreadSheetId, System.String[] sheetNames) [0x00000] in <00000000000000000000000000000000>:0
at GoogleSheetsLoader.Get (System.String spreadSheetId, System.String[] sheetNames) [0x00000] in <00000000000000000000000000000000>:0
GoogleSheetsLoader.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Google.Apis.Services;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
public class GoogleSheetsLoader {
private readonly string applicationName;
private readonly string apiKey;
public GoogleSheetsLoader(string applicationName, string apiKey) {
this.applicationName = applicationName;
this.apiKey = apiKey;
}
public List<string[][]> Get(string spreadSheetId, string[] sheetNames) {
var metadata = Load(spreadSheetId, sheetNames);
return metadata.Select(ParseSheetContents).ToList();
}
private ValueRange[] Load(string spreadSheetId, string[] sheetNames) {
BaseClientService.Initializer initializer = new BaseClientService.Initializer {
ApplicationName = applicationName,
ApiKey = apiKey,
};
SheetsService service = new SheetsService(initializer);
SpreadsheetsResource.ValuesResource.BatchGetRequest request = service.Spreadsheets.Values.BatchGet(spreadSheetId);
request.Ranges = new List<string>(sheetNames);
request.MajorDimension = SpreadsheetsResource.ValuesResource.BatchGetRequest.MajorDimensionEnum.ROWS;
request.ValueRenderOption = SpreadsheetsResource.ValuesResource.BatchGetRequest.ValueRenderOptionEnum.FORMATTEDVALUE;
request.DateTimeRenderOption = SpreadsheetsResource.ValuesResource.BatchGetRequest.DateTimeRenderOptionEnum.FORMATTEDSTRING;
try {
BatchGetValuesResponse response = request.Execute();
return response.ValueRanges.ToArray();
}
catch (Exception e) {
throw new Exception($"Load google sheets failed, check sheet names first.\n{e}");
}
}
private string[][] ParseSheetContents(ValueRange sheetMetadata) {
var columnRange = sheetMetadata.Values[0].Count;
return sheetMetadata.Values
.Select(row => ParseRowContents(row, columnRange))
.Where(data => !string.IsNullOrEmpty(data[0]))
.ToArray();
}
private string[] ParseRowContents(IList<object> metadata, int columnRange) {
var result = metadata
.Select(obj => obj.ToString())
.ToArray();
var dataLength = result.Length;
if (dataLength < columnRange) {
var emptyArray = Enumerable.Range(0, columnRange - dataLength)
.Select(_ => "")
.ToArray();
result = result.Concat(emptyArray).ToArray();
}
return result;
}
}
google-sheets-api.png
(38.8 kB)
Comment
Your answer

Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Return a string[] of all the scenes in the build settings 1 Answer
Insert HTML iframe at Unity 0 Answers
Built project, now scripts are missing. 2 Answers