Skip to content

Commit 522eb5b

Browse files
authored
Merge pull request #13 from FranzDiebold/feat/v-2-0-0-switch-to-google-sheets-api
Feat/v 2 0 0 switch to google sheets api
2 parents 118005a + b57c0f1 commit 522eb5b

16 files changed

+291
-517
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ jobs:
1414
- uses: actions/checkout@v2
1515
with:
1616
persist-credentials: false
17-
- uses: actions/setup-node@v1
17+
- uses: actions/setup-node@v2
1818
with:
19-
node-version: "12.x"
19+
node-version: "14"
2020
registry-url: "https://registry.npmjs.org"
2121
- run: make install
2222
- run: make test-lib-ci
@@ -31,9 +31,9 @@ jobs:
3131
- uses: actions/checkout@v2
3232
with:
3333
persist-credentials: false
34-
- uses: actions/setup-node@v1
34+
- uses: actions/setup-node@v2
3535
with:
36-
node-version: "12.x"
36+
node-version: "14"
3737
registry-url: "https://registry.npmjs.org"
3838
- run: make install
3939
- run: make publish-lib
@@ -47,9 +47,9 @@ jobs:
4747
- uses: actions/checkout@v2
4848
with:
4949
persist-credentials: false
50-
- uses: actions/setup-node@v1
50+
- uses: actions/setup-node@v2
5151
with:
52-
node-version: "12.x"
52+
node-version: "14"
5353
registry-url: "https://registry.npmjs.org"
5454
- run: make install
5555
- run: make build-lib

README.md

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,26 @@ const attributesMapping = {
2222
name: "Name",
2323
email: "Email Address",
2424
contact: {
25-
_prefix: "Contact",
25+
_prefix: "Contact ",
2626
street: "Street",
2727
streetNumber: "Street Number",
2828
zip: "ZIP",
2929
city: "City",
3030
},
3131
skills: {
32-
_prefix: "Skill",
32+
_prefix: "Skill ",
3333
_listField: true,
3434
},
3535
};
3636
```
3737

3838
```ts
3939
googleSheetsDbService
40-
.get("1gSc_7WCmt-HuSLX01-Ev58VsiFuhbpYVo8krbPCvvqA", 1, attributesMapping)
40+
.get(
41+
"1gSc_7WCmt-HuSLX01-Ev58VsiFuhbpYVo8krbPCvvqA",
42+
"Characters",
43+
attributesMapping
44+
)
4145
.subscribe((characters: object[]) => {
4246
// Use the characters here
4347
});
@@ -65,27 +69,42 @@ npm install ng-google-sheets-db
6569
- You may have an _active_ column, with which you can enable or disable rows/entries.
6670
- A Google Sheets demo spreadsheet is available [here](https://docs.google.com/spreadsheets/d/1gSc_7WCmt-HuSLX01-Ev58VsiFuhbpYVo8krbPCvvqA).
6771
2. Share your sheet:
68-
- [File] → [Publish to the web] → (Entire Document, Web page) [Publish]
72+
- [File] → [Share] → On the bottom of the modal at "Get Link" click [Change to anyone with the link] to be "Viewer".
6973
- Get the _Spreadsheet ID_ (i.e. `1gSc_7WCmt-HuSLX01-Ev58VsiFuhbpYVo8krbPCvvqA`): It is part of the Google spreadsheet URL.
70-
- Get the _Worksheet ID_: The Worksheet IDs are increasing numbers, starting at 1.
74+
- Get the _Sheet Name_: The name of the worksheet can be found at the bottom of your Google spreadsheet.
7175
3. Optional: It may be a good idea to enable [2-Step Verification](https://www.google.com/landing/2step/) for your Google account, if you have not done it yet :wink:.
7276

77+
### Google Cloud Platform (GCP)
78+
79+
A good overview guide is the [Get started as a Workspace developer](https://developers.google.com/workspace/guides/getstarted-overview).
80+
81+
1. Create a new project in the [Google Cloud Console](https://console.cloud.google.com/).
82+
2. Enable Google Sheets API: [APIs & Services] → [Enable APIs and Services] → Search for "Google Sheets API" → [ENABLE].
83+
3. Create an _API key_: [APIs & Services] → [Credentials] → [+ CREATE CREDENTIALS] → [API key] → [RESTRICT KEY] → In "Application restrictions" choose "HTTP referrers (web sites)" with "Website restrictions" and in "API restrictions" choose "Restrict key" and select "Google Sheets API" → [SAVE].
84+
4. Get the generated API key.
85+
7386
### Angular
7487

7588
Add `GoogleSheetsDbService` to your app's module as a provider and Angular's `HttpClientModule` to the imports:
7689

7790
```typescript
7891
import { HttpClientModule } from '@angular/common/http';
7992

80-
import { GoogleSheetsDbService } from 'ng-google-sheets-db';
93+
import { API_KEY, GoogleSheetsDbService } from 'ng-google-sheets-db';
8194

8295
@NgModule({
8396
...
8497
imports: [
8598
HttpClientModule,
8699
...
87100
],
88-
providers: [ GoogleSheetsDbService ],
101+
providers: [
102+
{
103+
provide: API_KEY,
104+
useValue: <YOUR_GOOGLE_SHEETS_API_KEY>,
105+
},
106+
GoogleSheetsDbService
107+
],
89108
...
90109
})
91110
export class AppModule { }
@@ -105,7 +124,7 @@ export class YourComponent implements OnInit {
105124
constructor(private googleSheetsDbService: GoogleSheetsDbService) { }
106125

107126
ngOnInit(): void {
108-
this.characters$ = this.googleSheetsDbService.get<Character>('1gSc_7WCmt-HuSLX01-Ev58VsiFuhbpYVo8krbPCvvqA', 1, characterAttributesMapping);
127+
this.characters$ = this.googleSheetsDbService.get<Character>('1gSc_7WCmt-HuSLX01-Ev58VsiFuhbpYVo8krbPCvvqA', "Characters", characterAttributesMapping);
109128
}
110129
```
111130
@@ -119,14 +138,14 @@ const attributesMapping = {
119138
name: "Name",
120139
email: "Email Address",
121140
contact: {
122-
_prefix: "Contact",
141+
_prefix: "Contact ",
123142
street: "Street",
124143
streetNumber: "Street Number",
125144
zip: "ZIP",
126145
city: "City",
127146
},
128147
skills: {
129-
_prefix: "Skill",
148+
_prefix: "Skill ",
130149
_listField: true,
131150
},
132151
};
@@ -136,34 +155,34 @@ For example, the Google spreadsheet column _Email Address_ is mapped to the outc
136155
137156
#### Nested objects
138157
139-
`contact` is an example of a nested object. You may define a `_prefix` as a prefix for all columns of the nested object.
158+
`contact` is an example of a nested object. You may define a `_prefix` as a prefix for all columns of the nested object. Please note that the `_prefix` may need a trailing whitespace.
140159
141160
#### Lists
142161
143-
`skills` is an example of a list. You need to set `_listField` and a `_prefix` for all columns of the list. In this example, all columns starting with _Skill_ and an increasing number are part of the list, i.e. _Skill 1_, _Skill 2_, etc.
162+
`skills` is an example of a list. You need to set `_listField` and a `_prefix` for all columns of the list. In this example, all columns starting with _Skill _ and an increasing number are part of the list, i.e. _Skill 1_, _Skill 2_, etc. Please note that the `_prefix` may need a trailing whitespace.
144163
145164
## Methods
146165
147-
### get<T>(spreadsheetId: string, worksheetId: string | number, attributesMapping: object | string[]): Observable<T[]>
166+
### get<T>(spreadsheetId: string, worksheetName: string, attributesMapping: object | string[]): Observable<T[]>
148167
149168
```typescript
150169
const allCharacters$: Observable<Character> =
151170
googleSheetsDbService.get<Character>(
152171
"1gSc_7WCmt-HuSLX01-Ev58VsiFuhbpYVo8krbPCvvqA",
153-
1,
172+
"Characters",
154173
attributesMapping
155174
);
156175
```
157176
158177
Get all rows from the Google spreadsheet as an `Observable` of objects or a given type as type variable `T`.
159178
160-
### getActive<T>(spreadsheetId: string, worksheetId: string | number, attributesMapping: object | string[], isActiveColumnName: string = 'is_active', activeValues: string[] | string = null): Observable<T[]>
179+
### getActive<T>(spreadsheetId: string, worksheetName: string, attributesMapping: object | string[], isActiveColumnName: string = 'is_active', activeValues: string[] | string = null): Observable<T[]>
161180
162181
```typescript
163182
const activeCharacters$: Observable<Character> =
164183
googleSheetsDbService.getActive<Character>(
165184
"1gSc_7WCmt-HuSLX01-Ev58VsiFuhbpYVo8krbPCvvqA",
166-
1,
185+
"Characters",
167186
attributesMapping,
168187
"Active"
169188
);

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ng-google-sheets-db-library",
3-
"version": "1.1.0",
3+
"version": "2.0.0",
44
"description": "Use Google Sheets as your (read-only) backend for your Angular app!",
55
"author": {
66
"name": "Franz Diebold"

projects/demo/src/app/app.component.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { TestBed, waitForAsync } from '@angular/core/testing';
22
import { HttpClientTestingModule } from '@angular/common/http/testing';
3+
import { API_KEY } from 'ng-google-sheets-db';
34

45
import { AppComponent } from './app.component';
56

@@ -9,6 +10,12 @@ describe('AppComponent', () => {
910
TestBed.configureTestingModule({
1011
declarations: [AppComponent],
1112
imports: [HttpClientTestingModule],
13+
providers: [
14+
{
15+
provide: API_KEY,
16+
useValue: 'some-fake-test-api-key',
17+
},
18+
],
1219
}).compileComponents();
1320
})
1421
);

projects/demo/src/app/app.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class AppComponent implements OnInit {
2020
ngOnInit(): void {
2121
this.characters$ = this.googleSheetsDbService.getActive<Character>(
2222
environment.characters.spreadsheetId,
23-
environment.characters.worksheetId,
23+
environment.characters.worksheetName,
2424
characterAttributesMapping,
2525
'Active'
2626
);

projects/demo/src/app/app.module.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@ import { BrowserModule } from '@angular/platform-browser';
22
import { NgModule } from '@angular/core';
33
import { HttpClientModule } from '@angular/common/http';
44

5-
import { GoogleSheetsDbService } from 'ng-google-sheets-db';
5+
import { API_KEY, GoogleSheetsDbService } from 'ng-google-sheets-db';
66

7+
import { environment } from '../environments/environment';
78
import { AppComponent } from './app.component';
89

910
@NgModule({
1011
declarations: [AppComponent],
1112
imports: [BrowserModule, HttpClientModule],
12-
providers: [GoogleSheetsDbService],
13+
providers: [
14+
{
15+
provide: API_KEY,
16+
useValue: environment.googleSheetsApiKey,
17+
},
18+
GoogleSheetsDbService,
19+
],
1320
bootstrap: [AppComponent],
1421
})
1522
export class AppModule {}

projects/demo/src/app/character.model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ export const characterAttributesMapping = {
33
name: 'Name',
44
email: 'Email Address',
55
contact: {
6-
_prefix: 'Contact',
6+
_prefix: 'Contact ',
77
street: 'Street',
88
streetNumber: 'Street Number',
99
zip: 'ZIP',
1010
city: 'City',
1111
},
1212
skills: {
13-
_prefix: 'Skill',
13+
_prefix: 'Skill ',
1414
_listField: true,
1515
},
1616
};
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
export const environment = {
22
production: true,
3+
googleSheetsApiKey: 'AIzaSyD-Nkv-g1682kKdauukFPmOCNaQk0wP540',
34
characters: {
45
spreadsheetId: '1gSc_7WCmt-HuSLX01-Ev58VsiFuhbpYVo8krbPCvvqA',
5-
worksheetId: 1,
6+
worksheetName: 'Characters',
67
},
78
};

projects/demo/src/environments/environment.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
export const environment = {
66
production: false,
7+
googleSheetsApiKey: 'AIzaSyD-Nkv-g1682kKdauukFPmOCNaQk0wP540',
78
characters: {
89
spreadsheetId: '1gSc_7WCmt-HuSLX01-Ev58VsiFuhbpYVo8krbPCvvqA',
9-
worksheetId: 1,
10+
worksheetName: 'Characters',
1011
},
1112
};
1213

0 commit comments

Comments
 (0)