GoogleSpreadSheetDataSource class

Create a data source adapter based on a Google spreadsheet.

Prepare a Google Spreadsheet in advance by following the steps below.

  1. You can create your own spreadsheets from Template for Collection or [Template for Documents Copy the spreadsheet from Template for Collection or Template for Documents to your Copy the spreadsheet to your Google Drive.
    • You can copy from File -> Make a copy.
  2. In the copied spreadsheet, click File -> Share -> Share with others.
  3. In the Share "(the name of the spreadsheet you created)" window, change General access to Anyone with the link.
  4. Copy the URL displayed in your browser (e.g., https://docs.google.com/spreadsheets/d/1bfNX8clPH9PFOcfFIStNCGNGjeCKwGv-24iChSJn8yM/edit#gid=0) and paste it into this and paste it into the source field of this annotation.

You can create a data model using Google Spreadsheets as the data source by annotating it with CollectionModelPath or DocumentModelPath as shown in the example below. The type of data to be defined is determined by the CollectionModelPath or DocumentModelPath, so please select the appropriate template to handle.

You can also explicitly update the content by changing the number of version.

If version is not changed, the spreadsheet contents are cached.

Googleスプレッドシートを元にデータソースアダプターを作成します。

事前に下記の手順でGoogleスプレッドシートの準備を行います。

  1. Collection用のテンプレートもしくはDocuments用のテンプレートからスプレッドシートを自分のGoogleドライブにコピーします。
    • ファイル -> コピーの作成からコピーが可能です。
  2. コピーしたスプレッドシート内でファイル -> 共有 -> 他のユーザーと共有をクリックします。
  3. (作成したスプレッドシート名)を共有ウィンドウ内で、一般的なアクセスリンクを知っている全員に変更します。
  4. ブラウザで表示されているURL(例:https://docs.google.com/spreadsheets/d/1bfNX8clPH9PFOcfFIStNCGNGjeCKwGv-24iChSJn8yM/edit#gid=0)をコピーしこのアノテーションのsourceに貼り付けます。

下記の例のようにアノテーションを付与してCollectionModelPathもしくはDocumentModelPathを合わせて付与することでGoogleスプレッドシートをデータソースとしたデータモデルを作成することができます。 定義するデータの種類はCollectionModelPathもしくはDocumentModelPathによって決定されますので扱うテンプレートは適切に選択してください。

またversionの数を変更することにより明示的に内容を更新することができます。

versionを変更しない場合はスプレッドシートの内容がキャッシュされます。

@freezed
@formValue
@immutable
@CollectionModelPath("category")
@GoogleSpreadSheetDataSource(
  "https://docs.google.com/spreadsheets/d/1bfNX8clPH9PFOcfFIStNCGNGjeCKwGv-24iChSJn8yM/edit#gid=0",
  version: 1,
)
class CategoryModel with _$CategoryModel {
  const factory CategoryModel({
    @Default("") String name,
    @Default("") String description,
  }) = _CategoryModel;
  const CategoryModel._();

  factory CategoryModel.fromJson(Map<String, Object?> json) => _$CategoryModelFromJson(json);

  static const document = _$CategoryModelDocumentQuery();

  static const collection = _$CategoryModelCollectionQuery();
}
Available Extensions

Constructors

GoogleSpreadSheetDataSource(String source, {required int version, String idKey = "id", bool direct = false, GoogleSpreadSheetDataSourceDirection direction = GoogleSpreadSheetDataSourceDirection.horizontal})
Create a data source adapter based on a Google spreadsheet.
const

Properties

direct bool
true if you want to download directly from the URL when the app is launched.
final
direction GoogleSpreadSheetDataSourceDirection
Orientation for using data source for documents at GoogleSpreadSheetDataSource.
final
hashCode int
The hash code for this object.
no setterinherited
idKey String
Key for ID.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
source String
Google Spreadsheet URL.
final
version int
Google Spreadsheet Version.
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited