Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hituziando/spreadsheetdatabase
SpreadsheetDatabase is a simple database using Google Sheets API v4.
https://github.com/hituziando/spreadsheetdatabase
database google java sheets-api spreadsheet
Last synced: about 1 month ago
JSON representation
SpreadsheetDatabase is a simple database using Google Sheets API v4.
- Host: GitHub
- URL: https://github.com/hituziando/spreadsheetdatabase
- Owner: HituziANDO
- License: mit
- Created: 2014-02-16T08:47:16.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2019-01-13T02:26:18.000Z (almost 6 years ago)
- Last Synced: 2023-08-14T03:41:10.895Z (over 1 year ago)
- Topics: database, google, java, sheets-api, spreadsheet
- Language: Java
- Homepage:
- Size: 3.05 MB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SpreadsheetDatabase
***SpreadsheetDatabase is a simple database using [Google Sheets API v4](https://developers.google.com/sheets/api/).***
## Install
1. Sets up your project with reference to [Java Quickstart: Step1-Step2](https://developers.google.com/sheets/api/quickstart/java)
1. Puts `SpreadsheetDatabaseJava/src/main/java/spreadsheetdb/v4/*` in your project## Usage
1. Create Database
```java
private static final String APPLICATION_NAME = "Your App Name";
private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
private static final SpreadsheetDatabase.CredentialsProvider CREDENTIALS_PROVIDER = new SpreadsheetDatabase.CredentialsProvider() {
@Override
public InputStream getCredentials() {
return Main.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
}
};public static void main(String... args) throws GeneralSecurityException, IOException {
SpreadsheetDatabase db = SpreadsheetDatabase.newPersonalDatabase(APPLICATION_NAME, CREDENTIALS_PROVIDER);
...
}
```
1. Create Table If Not Exist
```java
db.createTableRequest("member", Arrays.asList("name", "country", "address1", "tel")).execute();
```
1. Insert
```java
db.updateRequest("member")
.insert(new Record(Arrays.asList("Aiko", "Japan", "Tokyo", 111)))
.insert(new Record(Arrays.asList("Bob", "USA", "New York", 222)))
.execute();
```
1. Select All
```java
List records = db.queryRequest("member").all().execute();
```
1. Find By Row Index
```java
Record record1 = db.queryRequest("member").findByRowIndex(1).execute().get(0);
```
1. Update
```java
record1.setValues(Arrays.asList("Akko", "Japan", "Kyoto", 333));
db.updateRequest("member").update(record1).execute();
```
1. Delete
```java
db.deleteRequest("member")
.setRecords(Arrays.asList(new Record(1)))
.execute();
```
1. Truncate
```java
db.truncateRequest("member").execute();
```
1. Drop Table
```java
db.dropTableRequest("member").execute();
```
1. Migrate
```java
int newSchemaVersion = 2;
if (db.getMetadata().getSchemaVersion() < newSchemaVersion) {
db.migrateRequest("member", Arrays.asList("name", "country", "address1", "tel", "score"), newSchemaVersion).execute(new MigrateRequest.MigrationListener() {
@Override
public List onMigrate(Table table, List newColumns, List oldRecords) {
for (Record record : oldRecords) {
record.setValues(Arrays.asList(
record.getString(table.getColumnIndex("name")),
record.getString(table.getColumnIndex("country")),
record.getString(table.getColumnIndex("address1")),
record.getInt(table.getColumnIndex("tel")),
99.9 // Set default value
));
}
// Return new values
return oldRecords;
}
});
}
```
More info, see my [sample code](https://github.com/HituziANDO/GoogleDriveSpreadsheet/blob/master/SpreadsheetDatabaseJava/src/main/java/Main.java).