Skip to content

Commit

Permalink
Merge pull request #641 from FastReports/sync_branch_2023.3.10
Browse files Browse the repository at this point in the history
FastReport.OpenSource 2023.3.10
  • Loading branch information
0legK authored Nov 10, 2023
2 parents 6863e4d + 6dc9f8c commit 764e91a
Show file tree
Hide file tree
Showing 35 changed files with 1,894 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
using FastReport.Utils;

namespace FastReport.Data
{
public class GoogleAssemblyInitializer : AssemblyInitializerBase
{
public GoogleAssemblyInitializer()
{
RegisteredObjects.AddConnection(typeof(GoogleSheetsDataConnection));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Google.Apis.Auth.OAuth2;
using Google.Apis.Sheets.v4;
using System;
using System.IO;
using System.Threading;

namespace FastReport.Data
{
public class GoogleAuthService
{
#region Public Methods

/// <summary>
/// Getting a token by connecting to a .json file
/// </summary>
/// <param name="path">Path to the location of the .json file containing the Google client secret data</param>
/// <returns>UserCredential with access token</returns>
public static UserCredential GetAccessToken(string path)
{
string[] scopes = new string[] { SheetsService.Scope.Spreadsheets };

using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.FromStream(stream).Secrets,
scopes,
"userName",
CancellationToken.None).Result;

return credential;
}
}

/// <summary>
/// Getting a token over an OAuth 2.0 connection
/// </summary>
/// <param name="clientId">Google Client ID</param>
/// <param name="clientSecret">Google Client Secret</param>
/// <returns>UserCredential with access token</returns>
public static UserCredential GetAccessToken(string clientId, string clientSecret)
{
string[] scopes = new string[] { SheetsService.Scope.Spreadsheets };

var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = clientId,
ClientSecret = clientSecret
},
scopes,
Environment.UserName,
CancellationToken.None).Result;

return credential;
}

#endregion Public Methods
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using System.Collections.Generic;
using System.Windows.Forms;

namespace FastReport.Data
{
/// <summary>
/// Provides connection, reading and writing data from Google Sheets
/// </summary>
public class GoogleSheets
{
#region Private Fields

private static SheetsService service;

#endregion Private Fields

#region Public Method

/// <summary>
/// Initializing the Sheets service with an Access Token
/// </summary>
/// <param name="credential">Contains an access token for the Google Sheets API</param>
public static void InitService(UserCredential credential)
{
service = new SheetsService(new BaseClientService.Initializer()
{
ApplicationName = " ",
HttpClientInitializer = credential,
});
}

/// <summary>
/// Initializing the Sheets service using an API key
/// </summary>
/// <param name="APIkey">Contains an API key to access the Google Sheets API</param>
/// <returns>SheetsService for performing operations with the Google Sheets API</returns>
public static SheetsService InitService(string APIkey)
{
service = new SheetsService(new BaseClientService.Initializer()
{
ApplicationName = " ",
ApiKey = APIkey,
});

return service;
}

/// <summary>
/// Read data from a sheet specifying a range of cells
/// </summary>
/// <param name="spreadsheetsId">Google Sheets Table ID</param>
/// <param name="range">Range of cells to be read</param>
/// <returns>A list of lists of objects (IList<IList<object>>) that contains the data read</returns>
public static IList<IList<object>> ReadData(string spreadsheetsId, string range)
{
var response = service.Spreadsheets.Values.Get(spreadsheetsId, range).Execute();

return response.Values;
}

/// <summary>
/// Read data from a sheet specifying a range of cells
/// </summary>
/// <param name="spreadsheetsId">Google Sheets Table ID</param>
/// <param name="startColumn">Range start column to be read</param>
/// <param name="endColumn">Range end column to be read</param>
/// <returns>A list of lists of objects (IList<IList<object>>) that contains the data read</returns>
public static IList<IList<object>> ReadData(string spreadsheetsId, string startColumn, string endColumn)
{
var range = startColumn + ":" + endColumn;

var response = service.Spreadsheets.Values.Get(spreadsheetsId, range).Execute();

return response.Values;
}

/// <summary>
/// Reading data from a table
/// </summary>
/// <param name="spreadsheetsId">Google Sheets Table ID</param>
/// <returns>Returns a Spreadsheet with information about the table</returns>
public static Spreadsheet ReadSpreadSheet (string spreadsheetsId)
{
var spreadsheet = service.Spreadsheets.Get(spreadsheetsId).Execute();

return spreadsheet;
}

#endregion Public Method
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using FastReport.Data.ConnectionEditors;
using FastReport.Utils;

namespace FastReport.Data
{
internal partial class GoogleSheetsConnectionEditor : ConnectionEditorBase
{
#region Fields

private bool updating;

#endregion Fields

#region Constructors
public GoogleSheetsConnectionEditor()
{
updating = true;
InitializeComponent();
CheckSignInGoogleAPI();
Localize();
updating = false;
}

#endregion Constructors

#region Events Handlers

private void btSignInGoogle_Click(object sender, EventArgs e)
{
using (SignInGoogle signInGoogle = new SignInGoogle())
{
signInGoogle.ShowDialog();
}
}

#endregion Events Handlers

#region Protected Methods

protected override string GetConnectionString()
{
GoogleSheetsConnectionStringBuilder builder = new GoogleSheetsConnectionStringBuilder();
builder.Sheets = tbGoogleId.Text;
builder.FieldNamesInFirstString = cbxFieldNames.Checked;
return builder.ToString();
}

protected override void SetConnectionString(string value)
{
GoogleSheetsConnectionStringBuilder builder = new GoogleSheetsConnectionStringBuilder(value);
tbGoogleId.Text = builder.Sheets;
cbxFieldNames.Checked = builder.FieldNamesInFirstString;
}

#endregion Protected Methods

#region Private Methods

private void Localize()
{
MyRes res = new MyRes("ConnectionEditors,GoogleSheets");
gbSelect.Text = res.Get("ConfigureDatabase");
lblSelectGId.Text = res.Get("SelectSheetsId");
cbxFieldNames.Text = res.Get("FieldNames");
btSignInGoogle.Text = res.Get("SignIn");
}

private void CheckSignInGoogleAPI ()
{
XmlItem xi = Config.Root.FindItem("GoogleSheets").FindItem("StorageSettings");
string id = xi.GetProp("ClientId");
string secret = xi.GetProp("ClientSecret");
string pathToJson = xi.GetProp("PathToJson");
string apiKey = xi.GetProp("ApiKey");
if (String.IsNullOrEmpty(id) && String.IsNullOrEmpty(secret) && String.IsNullOrEmpty(pathToJson) && String.IsNullOrEmpty(apiKey))
{
using (SignInGoogle signInGoogle = new SignInGoogle())
signInGoogle.ShowDialog();
}
}

#endregion Private Methods

#region Public Methods

public override void UpdateDpiDependencies()
{
base.UpdateDpiDependencies();
}

#endregion Public Methods
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 764e91a

Please sign in to comment.