GoogleSpreadSheetDataSource constructor

const GoogleSpreadSheetDataSource(
  1. String source, {
  2. required int version,
  3. String idKey = "id",
  4. bool direct = false,
  5. GoogleSpreadSheetDataSourceDirection direction = GoogleSpreadSheetDataSourceDirection.horizontal,
})

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();
}

Implementation

const GoogleSpreadSheetDataSource(
  this.source, {
  required this.version,
  this.idKey = "id",
  this.direct = false,
  this.direction = GoogleSpreadSheetDataSourceDirection.horizontal,
});