Export DataSet to GoogleSheet
This commit is contained in:
@@ -7,7 +7,7 @@ import { DataRow } from './dataRow.model copy';
|
|||||||
|
|
||||||
export class DataSet extends Base {
|
export class DataSet extends Base {
|
||||||
number?: Number;
|
number?: Number;
|
||||||
source?: string;
|
source?: string;
|
||||||
name?: string;
|
name?: string;
|
||||||
dataRows: DataRow[] = [];
|
dataRows: DataRow[] = [];
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ export class DataSet extends Base {
|
|||||||
super();
|
super();
|
||||||
Object.assign(this, data);
|
Object.assign(this, data);
|
||||||
}
|
}
|
||||||
override deserialize(input: any): this {
|
override deserialize(input: any): this {
|
||||||
Object.assign(this, Object.assign(this, super.deserialize(input)));
|
Object.assign(this, Object.assign(this, super.deserialize(input)));
|
||||||
if (this.dataRows) { this.dataRows = this.dataRows.map(x => new DataRow().deserialize(x)); }
|
if (this.dataRows) { this.dataRows = this.dataRows.map(x => new DataRow().deserialize(x)); }
|
||||||
return this;
|
return this;
|
||||||
@@ -34,7 +34,7 @@ export class DataSet extends Base {
|
|||||||
modifiedAt: '',
|
modifiedAt: '',
|
||||||
createdBy: '',
|
createdBy: '',
|
||||||
modifiedBy: '',
|
modifiedBy: '',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
fillForm(form: UntypedFormGroup) {
|
fillForm(form: UntypedFormGroup) {
|
||||||
form.patchValue(this);
|
form.patchValue(this);
|
||||||
@@ -53,21 +53,21 @@ export class DataSet extends Base {
|
|||||||
//API Actions
|
//API Actions
|
||||||
static add(input: DataSet, _http: HttpClient): Promise<string> {
|
static add(input: DataSet, _http: HttpClient): Promise<string> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
_http.post<string>(`${environment.api.url}/datasets`, {...input.serialize(), }).subscribe({
|
_http.post<string>(`${environment.api.url}/datasets`, { ...input.serialize(), }).subscribe({
|
||||||
next: (data) => resolve(data),
|
next: (data) => resolve(data),
|
||||||
error: (e) => reject(e)
|
error: (e) => reject(e)
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
static getList(_http: HttpClient): any {
|
static getList(_http: HttpClient): any {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
_http.get<DataSet[]>(`${environment.api.url}/datasets`)
|
_http.get<DataSet[]>(`${environment.api.url}/datasets`)
|
||||||
.pipe(map(data => data.map(x => new DataSet().deserialize(x))))
|
.pipe(map(data => data.map(x => new DataSet().deserialize(x))))
|
||||||
.subscribe({
|
.subscribe({
|
||||||
next: (data) => resolve(data),
|
next: (data) => resolve(data),
|
||||||
error: (e) => reject(e)
|
error: (e) => reject(e)
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
static getById(id: string, _http: HttpClient): Promise<DataSet> {
|
static getById(id: string, _http: HttpClient): Promise<DataSet> {
|
||||||
@@ -83,20 +83,31 @@ export class DataSet extends Base {
|
|||||||
formData.append(file.name, file);
|
formData.append(file.name, file);
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
_http.post<DataRow[]>(`${environment.api.url}/datasets/parseFile`, formData,
|
_http.post<DataRow[]>(`${environment.api.url}/datasets/parseFile`, formData,
|
||||||
).pipe(map(data => data.map(x => new DataRow().deserialize(x))))
|
).pipe(map(data => data.map(x => new DataRow().deserialize(x))))
|
||||||
.subscribe({
|
.subscribe({
|
||||||
next: (data) => {
|
next: (data) => {
|
||||||
resolve(data);
|
resolve(data);
|
||||||
},
|
},
|
||||||
error: (e) => reject(e)
|
error: (e) => reject(e)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
static parseGoogleSheet(sheetId: string, _http: HttpClient): Promise<DataRow[]> {
|
static parseGoogleSheet(sheetId: string, _http: HttpClient): Promise<DataRow[]> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
_http.get<DataRow[]>(`${environment.api.url}/datasets/parseGoogleSheet/${sheetId}`,
|
_http.get<DataRow[]>(`${environment.api.url}/datasets/parseGoogleSheet/${sheetId}`,
|
||||||
).pipe(map(data => data.map(x => new DataRow().deserialize(x))))
|
).pipe(map(data => data.map(x => new DataRow().deserialize(x))))
|
||||||
.subscribe({
|
.subscribe({
|
||||||
|
next: (data) => {
|
||||||
|
resolve(data);
|
||||||
|
},
|
||||||
|
error: (e) => reject(e)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
static exportToGoogleSheet(id: string, _http: HttpClient): Promise<boolean> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
_http.get<boolean>(`${environment.api.url}/datasets/exportToGoogleSheet/${id}`,
|
||||||
|
).subscribe({
|
||||||
next: (data) => {
|
next: (data) => {
|
||||||
resolve(data);
|
resolve(data);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
<mat-card appearance="outlined">
|
<mat-card appearance="outlined">
|
||||||
<mat-toolbar color="secondary">
|
<mat-toolbar color="secondary">
|
||||||
Szczegóły warstwy danych
|
Szczegóły warstwy danych
|
||||||
|
<span class="fill-to-right"></span>
|
||||||
|
<button mat-button (click)="export()">Eksportuj do Google</button>
|
||||||
</mat-toolbar>
|
</mat-toolbar>
|
||||||
<mat-card-content>
|
<mat-card-content>
|
||||||
<mat-grid-list cols="2" rowHeight="90px">
|
<mat-grid-list cols="2" rowHeight="90px">
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { HttpClient } from '@angular/common/http';
|
|||||||
import { Component, ViewChild } from '@angular/core';
|
import { Component, ViewChild } from '@angular/core';
|
||||||
import { UntypedFormGroup, UntypedFormBuilder } from '@angular/forms';
|
import { UntypedFormGroup, UntypedFormBuilder } from '@angular/forms';
|
||||||
import { MatPaginator } from '@angular/material/paginator';
|
import { MatPaginator } from '@angular/material/paginator';
|
||||||
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
||||||
import { MatSort } from '@angular/material/sort';
|
import { MatSort } from '@angular/material/sort';
|
||||||
import { MatTableDataSource } from '@angular/material/table';
|
import { MatTableDataSource } from '@angular/material/table';
|
||||||
import { Router, ActivatedRoute } from '@angular/router';
|
import { Router, ActivatedRoute } from '@angular/router';
|
||||||
@@ -32,7 +33,8 @@ export class DataSetDetailComponent {
|
|||||||
private router$: Router,
|
private router$: Router,
|
||||||
private http$: HttpClient,
|
private http$: HttpClient,
|
||||||
private route$: ActivatedRoute,
|
private route$: ActivatedRoute,
|
||||||
private auth$: AuthService
|
private auth$: AuthService,
|
||||||
|
private snackBar: MatSnackBar
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async ngOnInit() {
|
async ngOnInit() {
|
||||||
@@ -50,4 +52,11 @@ export class DataSetDetailComponent {
|
|||||||
trackByUid(index : number, item : DataRow) {
|
trackByUid(index : number, item : DataRow) {
|
||||||
return item.id;
|
return item.id;
|
||||||
}
|
}
|
||||||
|
async export() {
|
||||||
|
if (await DataSet.exportToGoogleSheet(this.document.id || "", this.http$)) {
|
||||||
|
this.snackBar.open("Plik został zapisany na dysku Google", "OK");
|
||||||
|
} else {
|
||||||
|
this.snackBar.open("Zapis się nie udał.", "OK");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ using System.IdentityModel.Tokens.Jwt;
|
|||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using WebAPI.dataParsers;
|
using WebAPI.dataParsers;
|
||||||
|
using WebAPI.Exports;
|
||||||
using WebAPI.Models;
|
using WebAPI.Models;
|
||||||
|
|
||||||
namespace WebAPI.Controllers
|
namespace WebAPI.Controllers
|
||||||
@@ -23,9 +24,14 @@ namespace WebAPI.Controllers
|
|||||||
{
|
{
|
||||||
private readonly AppDbContext db;
|
private readonly AppDbContext db;
|
||||||
private SpreadsheetsResource.ValuesResource googleSheetValues;
|
private SpreadsheetsResource.ValuesResource googleSheetValues;
|
||||||
public DataSetsController(AppDbContext _db, GoogleSheetsHelper _googleSheetsHelper) {
|
private GoogleDriveHelper googleDriveHelper;
|
||||||
|
public DataSetsController(
|
||||||
|
AppDbContext _db,
|
||||||
|
GoogleSheetsHelper _googleSheetsHelper,
|
||||||
|
GoogleDriveHelper _googleDriveHelper) {
|
||||||
db = _db;
|
db = _db;
|
||||||
googleSheetValues = _googleSheetsHelper.Service.Spreadsheets.Values;
|
googleSheetValues = _googleSheetsHelper.Service.Spreadsheets.Values;
|
||||||
|
googleDriveHelper = _googleDriveHelper;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
@@ -85,6 +91,18 @@ namespace WebAPI.Controllers
|
|||||||
var parser = new csvParser();
|
var parser = new csvParser();
|
||||||
return Ok(parser.parse(Request.Form.Files[0]));
|
return Ok(parser.parse(Request.Form.Files[0]));
|
||||||
}
|
}
|
||||||
|
[HttpGet]
|
||||||
|
[Route("exportToGoogleSheet/{id}")]
|
||||||
|
public IActionResult ExportToGoogleSheet(Guid id)
|
||||||
|
{
|
||||||
|
DataSet dataSet = db.DataSets
|
||||||
|
.Include(x => x.DataRows)
|
||||||
|
.Where(x => x.Id == id && !x.IsDeleted).First();
|
||||||
|
|
||||||
|
var export = new googleSheetExport(googleDriveHelper, googleSheetValues);
|
||||||
|
export.export(dataSet);
|
||||||
|
return Ok(true);
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
private DataSet AddDataSet(DataSet input, Guid currentUserId)
|
private DataSet AddDataSet(DataSet input, Guid currentUserId)
|
||||||
|
|||||||
50
WebAPI/Exports/googleSheet.export.cs
Normal file
50
WebAPI/Exports/googleSheet.export.cs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
using Google.Apis.Drive.v3.Data;
|
||||||
|
using Google.Apis.Sheets.v4;
|
||||||
|
using Google.Apis.Sheets.v4.Data;
|
||||||
|
using WebAPI.Models;
|
||||||
|
using static Google.Apis.Drive.v3.FilesResource;
|
||||||
|
|
||||||
|
namespace WebAPI.Exports
|
||||||
|
{
|
||||||
|
public class googleSheetExport
|
||||||
|
{
|
||||||
|
private GoogleDriveHelper googleDriveHelper;
|
||||||
|
private SpreadsheetsResource.ValuesResource googleSheetValues;
|
||||||
|
public googleSheetExport(GoogleDriveHelper _googleDriveHelper, SpreadsheetsResource.ValuesResource _googleSheetValues)
|
||||||
|
{
|
||||||
|
googleDriveHelper = _googleDriveHelper;
|
||||||
|
googleSheetValues = _googleSheetValues;
|
||||||
|
}
|
||||||
|
public void export(DataSet dataSet)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
List<IList<object>> data = new List<IList<object>>() { new List<object>() { dataSet.Name, dataSet.Number } };
|
||||||
|
foreach (DataRow dataRow in dataSet.DataRows)
|
||||||
|
{
|
||||||
|
data.Add(new List<object> { dataRow.Code, dataRow.Value });
|
||||||
|
}
|
||||||
|
|
||||||
|
Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File();
|
||||||
|
body.Name = $"export-{DateTime.Now}";
|
||||||
|
body.MimeType = "application/vnd.google-apps.spreadsheet";
|
||||||
|
body.Parents = new List<string> { "1Sar-9ux6GWlXKZiD-l1Mfh1gdsxZif3j" };
|
||||||
|
CreateRequest request = googleDriveHelper.Service.Files.Create(body);
|
||||||
|
var file = request.Execute();
|
||||||
|
|
||||||
|
string sheetId = file.Id;
|
||||||
|
var range = $"Sheet1!A1:B${data.Count}";
|
||||||
|
|
||||||
|
ValueRange valueRange = new ValueRange() { Values = data};
|
||||||
|
|
||||||
|
var updateRequest = googleSheetValues.Update(valueRange, sheetId, range);
|
||||||
|
updateRequest.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.RAW;
|
||||||
|
updateRequest.Execute();
|
||||||
|
|
||||||
|
} catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
35
WebAPI/GoogleDriveHelper.cs
Normal file
35
WebAPI/GoogleDriveHelper.cs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
using Google.Apis.Auth.OAuth2;
|
||||||
|
using Google.Apis.Drive.v3;
|
||||||
|
using Google.Apis.Services;
|
||||||
|
|
||||||
|
namespace WebAPI
|
||||||
|
{
|
||||||
|
public class GoogleDriveHelper
|
||||||
|
{
|
||||||
|
public DriveService Service { get; set; }
|
||||||
|
const string APPLICATION_NAME = "Diuna";
|
||||||
|
static readonly string[] Scopes = { DriveService.Scope.Drive };
|
||||||
|
public GoogleDriveHelper()
|
||||||
|
{
|
||||||
|
InitializeService();
|
||||||
|
}
|
||||||
|
private void InitializeService()
|
||||||
|
{
|
||||||
|
var credential = GetCredentialsFromFile();
|
||||||
|
Service = new DriveService(new BaseClientService.Initializer()
|
||||||
|
{
|
||||||
|
HttpClientInitializer = credential,
|
||||||
|
ApplicationName = APPLICATION_NAME
|
||||||
|
});
|
||||||
|
}
|
||||||
|
private GoogleCredential GetCredentialsFromFile()
|
||||||
|
{
|
||||||
|
GoogleCredential credential;
|
||||||
|
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
|
||||||
|
{
|
||||||
|
credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes);
|
||||||
|
}
|
||||||
|
return credential;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -54,6 +54,7 @@ builder.Services.AddEndpointsApiExplorer();
|
|||||||
builder.Services.AddSwaggerGen();
|
builder.Services.AddSwaggerGen();
|
||||||
|
|
||||||
builder.Services.AddSingleton(typeof(GoogleSheetsHelper));
|
builder.Services.AddSingleton(typeof(GoogleSheetsHelper));
|
||||||
|
builder.Services.AddSingleton(typeof(GoogleDriveHelper));
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CsvHelper" Version="30.0.1" />
|
<PackageReference Include="CsvHelper" Version="30.0.1" />
|
||||||
<PackageReference Include="Google.Apis.Auth" Version="1.58.0" />
|
<PackageReference Include="Google.Apis.Auth" Version="1.58.0" />
|
||||||
|
<PackageReference Include="Google.Apis.Drive.v3" Version="1.58.0.2859" />
|
||||||
<PackageReference Include="Google.Apis.Sheets.v4" Version="1.58.0.2826" />
|
<PackageReference Include="Google.Apis.Sheets.v4" Version="1.58.0.2826" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.0" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0" />
|
||||||
|
|||||||
Reference in New Issue
Block a user