1
javascripts APIを使用してGoogleシートにアクセスするためのラッパークラスを作成しました(Google APIのDartバージョンは機能しません)。コールバック関数では、データがDartiumのデバッガのbody
とresults
フィールドに格納されていますが、Dartからはアクセスできませんでした。どんな提案も大歓迎です。プログラムはDart 1.18を使用しています。js-interop:コールバック関数で入力データにアクセスする方法は?
Javascriptのコード
var googleStorage = new GoogleStorage();
function GoogleStorage() {
this.CLIENT_ID = 'XXXXXXX';
this.SCOPES = ["https://www.googleapis.com/auth/spreadsheets.readonly"];
}
GoogleStorage.prototype.authorize = function(handleAuthResult) {
gapi.auth.authorize({
client_id: this.CLIENT_ID, scope: this.SCOPES, immediate: false
}, handleAuthResult);
return false;
}
GoogleStorage.prototype.loadTraitUrl = function(handleLoadUrlResult) {
console.log("loadConnectionUrl");
var discoveryUrl = 'https://sheets.googleapis.com/$discovery/rest?version=v4';
gapi.client.load(discoveryUrl).then(handleLoadUrlResult);
}
GoogleStorage.prototype.listTraits = function(successFunc, errorFunc) {
console.log("listData");
gapi.client.sheets.spreadsheets.values.get({
spreadsheetId: 'YYYYY',
range: 'Taits!B8:AQ',
}).then(successFunc, errorFunc);
}
ダートラッパークラス
@JS()
library my_workbench.lib.common.google_storage;
import 'package:js/js.dart';
@JS('googleStorage')
class GoogleStorage {
external static authorize(Function handler);
external static traitLoadUrl(Function handler);
external static listTraits(Function success, Function error);
}
ダートプログラム
main() async {
querySelector("#my_button").onClick.listen(handleEvent);
}
void handleEvent(MouseEvent event) {
print("Button Clicked");
GoogleStorage.authorize(
allowInterop((var authResult) {
handleAuthResult(authResult);
})
);
}
void handleAuthResult(var authResult) {
print("Successful Authenticated: ${authResult}");
GoogleStorage.traitLoadUrl(
allowInterop((_) {
handleLoadUrlResult();
})
);
}
void handleLoadUrlResult() {
print("Successful loaded URL");
GoogleStorage.listTraits(
allowInterop((var response) {
// var range = response.body;
print("Success: ${response.body}");
}), allowInterop((var response) {
print("Success: ${response}");
}));
}