Skip to content

Commit

Permalink
Merge pull request #21 from kimsama/fix-googlespreadsheet
Browse files Browse the repository at this point in the history
Fix googlespreadsheet
  • Loading branch information
kimsama committed Aug 10, 2016
2 parents aa465fc + e6621cd commit b761cd2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 29 deletions.
Expand Up @@ -28,15 +28,10 @@ public IService SpreadsheetService
var docService = new DocumentsService("database");
docService.RequestFactory = requestFactory;

//@kims
//docService.setUserCredentials(username, password);
documentService = docService;

var ssService = new SpreadsheetsService("database");

//@kims
//ssService.setUserCredentials(username, password);

ssService.RequestFactory = requestFactory;
spreadsheetService = ssService;
}
Expand All @@ -51,26 +46,25 @@ public IService SpreadsheetService
}
}

public IDatabase GetDatabase(string name) {
/*
var feed = DocumentService.Query(new SpreadsheetQuery {TitleExact = true, Title = name });
if (feed.Entries.Count == 0)
return null;
return new Database(this, feed.Entries[0]);
*/
/// <summary>
/// @kims 2016.08.09. Added second parameter to pass error message by reference.
/// </summary>
/// <returns>Null, if any error has been occured.</returns>
public IDatabase GetDatabase(string name, ref string error) {

Google.GData.Spreadsheets.SpreadsheetQuery query = new Google.GData.Spreadsheets.SpreadsheetQuery();

// Make a request to the API and get all spreadsheets.
SpreadsheetsService service = spreadsheetService as SpreadsheetsService;

SpreadsheetFeed feed = service.Query(query);

if (feed.Entries.Count == 0)
{
//Debug.Log("There are no spreadsheets in your docs.");
error = @"There are no spreadsheets in your docs.";
return null;
}

//SpreadsheetEntry spreadsheet = null;
AtomEntry spreadsheet = null;
foreach (AtomEntry sf in feed.Entries)
{
Expand All @@ -80,7 +74,7 @@ public IService SpreadsheetService

if (spreadsheet == null)
{
//Debug.Log("There is no such spreadsheet with such title in your docs.");
error = @"There is no such spreadsheet with such title in your docs.";
return null;
}

Expand Down
Expand Up @@ -21,6 +21,6 @@ public interface IDatabaseClient {
/// </summary>
/// <param name="name"></param>
/// <returns>IDocument instance or null if not found</returns>
IDatabase GetDatabase(string name);
IDatabase GetDatabase(string name, ref string error);
}
}
43 changes: 31 additions & 12 deletions Assets/QuickSheet/GDataPlugin/Editor/GoogleMachineEditor.cs
Expand Up @@ -137,8 +137,10 @@ public override void OnInspectorGUI()

if (GUILayout.Button("Generate"))
{
if (Generate(this.machine) == null)
Debug.LogError("Failed to create a script from Google.");
if (Generate(this.machine) != null)
Debug.Log("Successfully generated!");
else
Debug.LogError("Failed to create a script from Google Spreadsheet.");
}
}

Expand All @@ -162,11 +164,15 @@ private void DoCellQuery(OnEachCell onCell)
if (string.IsNullOrEmpty(machine.WorkSheetName))
return;

var db = client.GetDatabase(machine.SpreadSheetName);
string error = string.Empty;
var db = client.GetDatabase(machine.SpreadSheetName, ref error);
if (db == null)
{
string message = string.Format(@"The given spreadsheet '{0}' or worksheet '{1}' does not exist. Note that the name is case sensitive.",
machine.SpreadSheetName, machine.WorkSheetName);
string message = string.Empty;
if (string.IsNullOrEmpty(error))
message = @"Unknown error.";
else
message = string.Format(@"{0}", error);
EditorUtility.DisplayDialog("Error", message, "OK");
return;
}
Expand Down Expand Up @@ -201,6 +207,9 @@ protected override void Import(bool reimport = false)
else
headerDic = machine.HeaderColumnList.ToDictionary(k => k.name);

List<HeaderColumn> tmpColumnList = new List<HeaderColumn>();

// query the first columns only.
DoCellQuery((cell) =>
{
Expand All @@ -211,19 +220,29 @@ protected override void Import(bool reimport = false)
if (int.Parse(m.Value) > 1)
return;
if (machine.HasHeadColumn() && reimport == false)
HeaderColumn column = new HeaderColumn();
column.name = cell.Value;
if (headerDic != null && headerDic.ContainsKey(cell.Value))
{
if (headerDic != null && headerDic.ContainsKey(cell.Value))
machine.HeaderColumnList.Add(new HeaderColumn { name = cell.Value, type = headerDic[cell.Value].type });
// if the column is already exist, copy its name and type from the exist one.
HeaderColumn h = machine.HeaderColumnList.Find(x => x.name == column.name);
if (h != null)
{
column.type = h.type;
column.isArray = h.isArray;
}
else
machine.HeaderColumnList.Add(new HeaderColumn { name = cell.Value, type = CellType.Undefined });
column.type = CellType.Undefined;
}
else
{
machine.HeaderColumnList.Add(new HeaderColumn { name = cell.Value, type = CellType.Undefined });
}
column.type = CellType.Undefined;
tmpColumnList.Add(column);
});

// update (all of settings are reset when it reimports)
machine.HeaderColumnList = tmpColumnList;

EditorUtility.SetDirty(machine);
AssetDatabase.SaveAssets();
}
Expand Down
Expand Up @@ -59,7 +59,8 @@ public class $ClassName : BaseGoogleEditor<$WorkSheetClassName>
$WorkSheetClassName targetData = target as $WorkSheetClassName;

var client = new DatabaseClient("", "");
var db = client.GetDatabase(targetData.SheetName) ?? client.CreateDatabase(targetData.SheetName);
string error = string.Empty;
var db = client.GetDatabase(targetData.SheetName, ref error);
var table = db.GetTable<$DataClassName>(targetData.WorksheetName) ?? db.CreateTable<$DataClassName>(targetData.WorksheetName);

List<$DataClassName> myDataList = new List<$DataClassName>();
Expand Down

0 comments on commit b761cd2

Please sign in to comment.