@azteam/google-api
Version:
N/A
563 lines (562 loc) • 33.4 kB
JavaScript
"use strict";
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _lodash = _interopRequireDefault(require("lodash"));
var _util = require("@azteam/util");
var _GoogleDriveAPI2 = _interopRequireWildcard(require("./GoogleDriveAPI"));
var _scope = require("./scope");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return r; }; var t, r = {}, e = Object.prototype, n = e.hasOwnProperty, o = "function" == typeof Symbol ? Symbol : {}, i = o.iterator || "@@iterator", a = o.asyncIterator || "@@asyncIterator", u = o.toStringTag || "@@toStringTag"; function c(t, r, e, n) { Object.defineProperty(t, r, { value: e, enumerable: !n, configurable: !n, writable: !n }); } try { c({}, ""); } catch (t) { c = function c(t, r, e) { return t[r] = e; }; } function h(r, e, n, o) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype); return c(a, "_invoke", function (r, e, n) { var o = 1; return function (i, a) { if (3 === o) throw Error("Generator is already running"); if (4 === o) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var u = n.delegate; if (u) { var c = d(u, n); if (c) { if (c === f) continue; return c; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (1 === o) throw o = 4, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = 3; var h = s(r, e, n); if ("normal" === h.type) { if (o = n.done ? 4 : 2, h.arg === f) continue; return { value: h.arg, done: n.done }; } "throw" === h.type && (o = 4, n.method = "throw", n.arg = h.arg); } }; }(r, n, new Context(o || [])), !0), a; } function s(t, r, e) { try { return { type: "normal", arg: t.call(r, e) }; } catch (t) { return { type: "throw", arg: t }; } } r.wrap = h; var f = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var l = {}; c(l, i, function () { return this; }); var p = Object.getPrototypeOf, y = p && p(p(x([]))); y && y !== e && n.call(y, i) && (l = y); var v = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(l); function g(t) { ["next", "throw", "return"].forEach(function (r) { c(t, r, function (t) { return this._invoke(r, t); }); }); } function AsyncIterator(t, r) { function e(o, i, a, u) { var c = s(t[o], t, i); if ("throw" !== c.type) { var h = c.arg, f = h.value; return f && "object" == _typeof(f) && n.call(f, "__await") ? r.resolve(f.__await).then(function (t) { e("next", t, a, u); }, function (t) { e("throw", t, a, u); }) : r.resolve(f).then(function (t) { h.value = t, a(h); }, function (t) { return e("throw", t, a, u); }); } u(c.arg); } var o; c(this, "_invoke", function (t, n) { function i() { return new r(function (r, o) { e(t, n, r, o); }); } return o = o ? o.then(i, i) : i(); }, !0); } function d(r, e) { var n = e.method, o = r.i[n]; if (o === t) return e.delegate = null, "throw" === n && r.i["return"] && (e.method = "return", e.arg = t, d(r, e), "throw" === e.method) || "return" !== n && (e.method = "throw", e.arg = new TypeError("The iterator does not provide a '" + n + "' method")), f; var i = s(o, r.i, e.arg); if ("throw" === i.type) return e.method = "throw", e.arg = i.arg, e.delegate = null, f; var a = i.arg; return a ? a.done ? (e[r.r] = a.value, e.next = r.n, "return" !== e.method && (e.method = "next", e.arg = t), e.delegate = null, f) : a : (e.method = "throw", e.arg = new TypeError("iterator result is not an object"), e.delegate = null, f); } function w(t) { this.tryEntries.push(t); } function m(r) { var e = r[4] || {}; e.type = "normal", e.arg = t, r[4] = e; } function Context(t) { this.tryEntries = [[-1]], t.forEach(w, this), this.reset(!0); } function x(r) { if (null != r) { var e = r[i]; if (e) return e.call(r); if ("function" == typeof r.next) return r; if (!isNaN(r.length)) { var o = -1, a = function e() { for (; ++o < r.length;) if (n.call(r, o)) return e.value = r[o], e.done = !1, e; return e.value = t, e.done = !0, e; }; return a.next = a; } } throw new TypeError(_typeof(r) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, c(v, "constructor", GeneratorFunctionPrototype), c(GeneratorFunctionPrototype, "constructor", GeneratorFunction), c(GeneratorFunctionPrototype, u, GeneratorFunction.displayName = "GeneratorFunction"), r.isGeneratorFunction = function (t) { var r = "function" == typeof t && t.constructor; return !!r && (r === GeneratorFunction || "GeneratorFunction" === (r.displayName || r.name)); }, r.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, c(t, u, "GeneratorFunction")), t.prototype = Object.create(v), t; }, r.awrap = function (t) { return { __await: t }; }, g(AsyncIterator.prototype), c(AsyncIterator.prototype, a, function () { return this; }), r.AsyncIterator = AsyncIterator, r.async = function (t, e, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(h(t, e, n, o), i); return r.isGeneratorFunction(e) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, g(v), c(v, u, "Generator"), c(v, i, function () { return this; }), c(v, "toString", function () { return "[object Generator]"; }), r.keys = function (t) { var r = Object(t), e = []; for (var n in r) e.unshift(n); return function t() { for (; e.length;) if ((n = e.pop()) in r) return t.value = n, t.done = !1, t; return t.done = !0, t; }; }, r.values = x, Context.prototype = { constructor: Context, reset: function reset(r) { if (this.prev = this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(m), !r) for (var e in this) "t" === e.charAt(0) && n.call(this, e) && !isNaN(+e.slice(1)) && (this[e] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0][4]; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(r) { if (this.done) throw r; var e = this; function n(t) { a.type = "throw", a.arg = r, e.next = t; } for (var o = e.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i[4], u = this.prev, c = i[1], h = i[2]; if (-1 === i[0]) return n("end"), !1; if (!c && !h) throw Error("try statement without catch or finally"); if (null != i[0] && i[0] <= u) { if (u < c) return this.method = "next", this.arg = t, n(c), !0; if (u < h) return n(h), !1; } } }, abrupt: function abrupt(t, r) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var n = this.tryEntries[e]; if (n[0] > -1 && n[0] <= this.prev && this.prev < n[2]) { var o = n; break; } } o && ("break" === t || "continue" === t) && o[0] <= r && r <= o[2] && (o = null); var i = o ? o[4] : {}; return i.type = t, i.arg = r, o ? (this.method = "next", this.next = o[2], f) : this.complete(i); }, complete: function complete(t, r) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && r && (this.next = r), f; }, finish: function finish(t) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var e = this.tryEntries[r]; if (e[2] === t) return this.complete(e[4], e[3]), m(e), f; } }, "catch": function _catch(t) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var e = this.tryEntries[r]; if (e[0] === t) { var n = e[4]; if ("throw" === n.type) { var o = n.arg; m(e); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(r, e, n) { return this.delegate = { i: x(r), r: e, n: n }, "next" === this.method && (this.arg = t), f; } }, r; }
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _superPropGet(t, o, e, r) { var p = _get(_getPrototypeOf(1 & r ? t.prototype : t), o, e); return 2 & r && "function" == typeof p ? function (t) { return p.apply(e, t); } : p; }
function _get() { return _get = "undefined" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) { var p = _superPropBase(e, t); if (p) { var n = Object.getOwnPropertyDescriptor(p, t); return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value; } }, _get.apply(null, arguments); }
function _superPropBase(t, o) { for (; !{}.hasOwnProperty.call(t, o) && null !== (t = _getPrototypeOf(t));); return t; }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
var SHEET_ENDPOINT = 'https://sheets.googleapis.com/v4';
var GoogleSheetAPI = /*#__PURE__*/function (_GoogleDriveAPI) {
function GoogleSheetAPI(accountType, credential) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
_classCallCheck(this, GoogleSheetAPI);
return _callSuper(this, GoogleSheetAPI, [accountType, _objectSpread(_objectSpread({}, credential), {}, {
scope: _scope.SHEET_SCOPE
}), options]);
}
_inherits(GoogleSheetAPI, _GoogleDriveAPI);
return _createClass(GoogleSheetAPI, [{
key: "getAuthLink",
value: function getAuthLink(redirectURI, state) {
var arrayScope = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [_scope.DRIVE_SCOPE, _scope.SHEET_SCOPE];
return _superPropGet(GoogleSheetAPI, "getAuthLink", this, 3)([redirectURI, state, arrayScope]);
}
}, {
key: "hasScope",
value: function () {
var _hasScope = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
var arrayScope,
_args = arguments;
return _regeneratorRuntime().wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
arrayScope = _args.length > 0 && _args[0] !== undefined ? _args[0] : [_scope.DRIVE_SCOPE, _scope.SHEET_SCOPE];
return _context.abrupt("return", _superPropGet(GoogleSheetAPI, "hasScope", this, 3)([arrayScope]));
case 2:
case "end":
return _context.stop();
}
}, _callee, this);
}));
function hasScope() {
return _hasScope.apply(this, arguments);
}
return hasScope;
}()
}, {
key: "getAllSpreadsheet",
value: function () {
var _getAllSpreadsheet = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
var containsName,
parentId,
query,
_args2 = arguments;
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
containsName = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : null;
parentId = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : null;
query = "mimeType='".concat(_GoogleDriveAPI2.DRIVE_FILE_TYPE.SHEET, "' and trashed=false and '").concat(parentId || 'root', "' in parents");
if (containsName) {
query = "name contains '".concat(containsName, "' and ").concat(query);
}
return _context2.abrupt("return", this._getAllItem({
q: query
}));
case 5:
case "end":
return _context2.stop();
}
}, _callee2, this);
}));
function getAllSpreadsheet() {
return _getAllSpreadsheet.apply(this, arguments);
}
return getAllSpreadsheet;
}()
}, {
key: "createSpreadsheet",
value: function () {
var _createSpreadsheet = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(title) {
var client, res;
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
while (1) switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return this._getAuthClient();
case 2:
client = _context3.sent;
_context3.next = 5;
return client.post("".concat(SHEET_ENDPOINT, "/spreadsheets"), {
properties: {
title: title
}
}, false);
case 5:
res = _context3.sent;
if (!res.error) {
_context3.next = 8;
break;
}
throw new Error(res.error.message);
case 8:
if (!res.spreadsheetId) {
_context3.next = 10;
break;
}
return _context3.abrupt("return", {
spreadsheetId: res.spreadsheetId,
spreadsheetUrl: res.spreadsheetUrl,
title: res.properties.title
});
case 10:
return _context3.abrupt("return", null);
case 11:
case "end":
return _context3.stop();
}
}, _callee3, this);
}));
function createSpreadsheet(_x) {
return _createSpreadsheet.apply(this, arguments);
}
return createSpreadsheet;
}()
}, {
key: "getSpreadsheet",
value: function () {
var _getSpreadsheet = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(spreadsheetId) {
var client, res;
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
while (1) switch (_context4.prev = _context4.next) {
case 0:
_context4.next = 2;
return this._getAuthClient();
case 2:
client = _context4.sent;
_context4.next = 5;
return client.get("".concat(SHEET_ENDPOINT, "/spreadsheets/").concat(spreadsheetId));
case 5:
res = _context4.sent;
if (!res.error) {
_context4.next = 8;
break;
}
throw new Error(res.error.message);
case 8:
return _context4.abrupt("return", {
title: res.properties.title,
sheets: res.sheets,
link: res.spreadsheetUrl
});
case 9:
case "end":
return _context4.stop();
}
}, _callee4, this);
}));
function getSpreadsheet(_x2) {
return _getSpreadsheet.apply(this, arguments);
}
return getSpreadsheet;
}()
}, {
key: "createSheet",
value: function () {
var _createSheet = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(spreadsheetId, sheetName) {
var client, res;
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
while (1) switch (_context5.prev = _context5.next) {
case 0:
_context5.next = 2;
return this._getAuthClient();
case 2:
client = _context5.sent;
_context5.next = 5;
return client.post("".concat(SHEET_ENDPOINT, "/spreadsheets/").concat(spreadsheetId, ":batchUpdate"), {
requests: [{
addSheet: {
properties: {
title: String(sheetName)
}
}
}]
}, false);
case 5:
res = _context5.sent;
if (!res.error) {
_context5.next = 8;
break;
}
throw new Error(res.error.message);
case 8:
return _context5.abrupt("return", res.replies[0].addSheet.properties);
case 9:
case "end":
return _context5.stop();
}
}, _callee5, this);
}));
function createSheet(_x3, _x4) {
return _createSheet.apply(this, arguments);
}
return createSheet;
}()
}, {
key: "deleteSheet",
value: function () {
var _deleteSheet = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(spreadsheetId, sheetId) {
var client, res;
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
while (1) switch (_context6.prev = _context6.next) {
case 0:
_context6.next = 2;
return this._getAuthClient();
case 2:
client = _context6.sent;
_context6.next = 5;
return client.post("".concat(SHEET_ENDPOINT, "/spreadsheets/").concat(spreadsheetId, ":batchUpdate"), {
requests: [{
deleteSheet: {
sheetId: sheetId
}
}]
}, false);
case 5:
res = _context6.sent;
if (!res.error) {
_context6.next = 8;
break;
}
throw new Error(res.error.message);
case 8:
case "end":
return _context6.stop();
}
}, _callee6, this);
}));
function deleteSheet(_x5, _x6) {
return _deleteSheet.apply(this, arguments);
}
return deleteSheet;
}()
}, {
key: "getSheetInfo",
value: function () {
var _getSheetInfo = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(spreadsheetId, sheetName) {
var _yield$this$getSpread, sheets, data;
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
while (1) switch (_context7.prev = _context7.next) {
case 0:
_context7.next = 2;
return this.getSpreadsheet(spreadsheetId);
case 2:
_yield$this$getSpread = _context7.sent;
sheets = _yield$this$getSpread.sheets;
data = sheets.find(function (obj) {
return obj.properties.title === sheetName;
});
if (!data) {
_context7.next = 7;
break;
}
return _context7.abrupt("return", data.properties);
case 7:
return _context7.abrupt("return", {});
case 8:
case "end":
return _context7.stop();
}
}, _callee7, this);
}));
function getSheetInfo(_x7, _x8) {
return _getSheetInfo.apply(this, arguments);
}
return getSheetInfo;
}()
}, {
key: "addRowsSheet",
value: function () {
var _addRowsSheet = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(spreadsheetId, sheetId, numberRows) {
var client, res;
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
while (1) switch (_context8.prev = _context8.next) {
case 0:
_context8.next = 2;
return this._getAuthClient();
case 2:
client = _context8.sent;
_context8.next = 5;
return client.post("".concat(SHEET_ENDPOINT, "/spreadsheets/").concat(spreadsheetId, ":batchUpdate"), {
requests: [{
appendDimension: {
sheetId: sheetId,
dimension: 'ROWS',
length: numberRows
}
}]
}, false);
case 5:
res = _context8.sent;
if (!res.error) {
_context8.next = 8;
break;
}
throw new Error(res.error.message);
case 8:
return _context8.abrupt("return", true);
case 9:
case "end":
return _context8.stop();
}
}, _callee8, this);
}));
function addRowsSheet(_x9, _x0, _x1) {
return _addRowsSheet.apply(this, arguments);
}
return addRowsSheet;
}()
}, {
key: "getValuesInSheet",
value: function () {
var _getValuesInSheet = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(spreadsheetId, sheetName, range) {
var isParseValues,
rangeQuery,
client,
res,
values,
_args9 = arguments;
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
while (1) switch (_context9.prev = _context9.next) {
case 0:
isParseValues = _args9.length > 3 && _args9[3] !== undefined ? _args9[3] : false;
rangeQuery = "".concat(sheetName, "!").concat(range);
_context9.next = 4;
return this._getAuthClient();
case 4:
client = _context9.sent;
_context9.next = 7;
return client.get("".concat(SHEET_ENDPOINT, "/spreadsheets/").concat(spreadsheetId, "/values/").concat(encodeURIComponent(rangeQuery)));
case 7:
res = _context9.sent;
if (!res.error) {
_context9.next = 10;
break;
}
throw new Error(res.error.message);
case 10:
values = res.values || [];
return _context9.abrupt("return", isParseValues ? GoogleSheetAPI.parseValues(values, range) : values);
case 12:
case "end":
return _context9.stop();
}
}, _callee9, this);
}));
function getValuesInSheet(_x10, _x11, _x12) {
return _getValuesInSheet.apply(this, arguments);
}
return getValuesInSheet;
}()
}, {
key: "validateSheet",
value: function () {
var _validateSheet = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee0(spreadsheetId, sheetName, range) {
var data,
isParseValues,
row,
_args0 = arguments;
return _regeneratorRuntime().wrap(function _callee0$(_context0) {
while (1) switch (_context0.prev = _context0.next) {
case 0:
data = _args0.length > 3 && _args0[3] !== undefined ? _args0[3] : [];
isParseValues = !Array.isArray(data);
_context0.next = 4;
return this.getValuesInSheet(spreadsheetId, sheetName, range, isParseValues);
case 4:
row = _context0.sent;
if (_lodash["default"].isEqual(row, data)) {
_context0.next = 7;
break;
}
return _context0.abrupt("return", false);
case 7:
return _context0.abrupt("return", true);
case 8:
case "end":
return _context0.stop();
}
}, _callee0, this);
}));
function validateSheet(_x13, _x14, _x15) {
return _validateSheet.apply(this, arguments);
}
return validateSheet;
}()
}, {
key: "insertToSheet",
value: function () {
var _insertToSheet = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee1(spreadsheetId, sheetName, range) {
var data,
rangeQuery,
client,
res,
_args1 = arguments;
return _regeneratorRuntime().wrap(function _callee1$(_context1) {
while (1) switch (_context1.prev = _context1.next) {
case 0:
data = _args1.length > 3 && _args1[3] !== undefined ? _args1[3] : [];
rangeQuery = "".concat(sheetName, "!").concat(range);
_context1.next = 4;
return this._getAuthClient();
case 4:
client = _context1.sent;
_context1.next = 7;
return client.post("".concat(SHEET_ENDPOINT, "/spreadsheets/").concat(spreadsheetId, "/values:batchUpdate"), {
valueInputOption: 'RAW',
data: [{
range: rangeQuery,
values: data
}]
}, false);
case 7:
res = _context1.sent;
if (!(res.totalUpdatedRows && res.totalUpdatedRows === data.length)) {
_context1.next = 10;
break;
}
return _context1.abrupt("return", true);
case 10:
console.error(res);
throw new Error("[insert]: Failed insert");
case 12:
case "end":
return _context1.stop();
}
}, _callee1, this);
}));
function insertToSheet(_x16, _x17, _x18) {
return _insertToSheet.apply(this, arguments);
}
return insertToSheet;
}()
}, {
key: "mergeCell",
value: function () {
var _mergeCell = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(spreadsheetId, sheetId, range) {
var _GoogleSheetAPI$parse, startColumnIndex, startRowIndex, endColumnIndex, endRowIndex, client, res;
return _regeneratorRuntime().wrap(function _callee10$(_context10) {
while (1) switch (_context10.prev = _context10.next) {
case 0:
_GoogleSheetAPI$parse = GoogleSheetAPI.parseRange(range);
startColumnIndex = _GoogleSheetAPI$parse.startColumnIndex;
startRowIndex = _GoogleSheetAPI$parse.startRowIndex;
endColumnIndex = _GoogleSheetAPI$parse.endColumnIndex;
endRowIndex = _GoogleSheetAPI$parse.endRowIndex;
_context10.next = 7;
return this._getAuthClient();
case 7:
client = _context10.sent;
_context10.next = 10;
return client.post("".concat(SHEET_ENDPOINT, "/spreadsheets/").concat(spreadsheetId, ":batchUpdate"), {
requests: [{
mergeCells: {
mergeType: 'MERGE_ALL',
range: {
sheetId: sheetId,
endRowIndex: endRowIndex,
endColumnIndex: endColumnIndex,
startColumnIndex: startColumnIndex,
startRowIndex: startRowIndex
}
}
}]
}, false);
case 10:
res = _context10.sent;
if (!res.error) {
_context10.next = 13;
break;
}
throw new Error("".concat(res.error.message));
case 13:
case "end":
return _context10.stop();
}
}, _callee10, this);
}));
function mergeCell(_x19, _x20, _x21) {
return _mergeCell.apply(this, arguments);
}
return mergeCell;
}()
}], [{
key: "parseRange",
value: function parseRange(range) {
var matches = range.match(/(\w+?)(\d*):(\w+?)(\d*)/),
colStart = matches[1] || null,
rowStart = matches[2] ? Number(matches[2]) : null,
colEnd = matches[3] || null,
rowEnd = matches[4] ? Number(matches[4]) : null;
if (colStart > colEnd || rowStart > rowEnd) {
throw new Error('[range]: Syntax error');
}
return {
colStart: colStart,
rowStart: rowStart,
colEnd: colEnd,
rowEnd: rowEnd,
startRowIndex: rowStart - 1,
endRowIndex: rowEnd,
startColumnIndex: colStart.toUpperCase().charCodeAt(0) - 65,
endColumnIndex: colEnd.toUpperCase().charCodeAt(0) - 65 + 1
};
}
}, {
key: "parseValues",
value: function parseValues(values, range) {
var _GoogleSheetAPI$parse2 = GoogleSheetAPI.parseRange(range),
colStart = _GoogleSheetAPI$parse2.colStart,
colEnd = _GoogleSheetAPI$parse2.colEnd,
rowStart = _GoogleSheetAPI$parse2.rowStart,
arrayColChar = (0, _util.genCharArray)(colStart, colEnd),
result = {};
for (var r = 0; r < values.length; r += 1) {
var rowArray = values[r];
for (var c = 0; c < rowArray.length; c += 1) {
var cell = rowArray[c],
cellKey = arrayColChar[c] + (rowStart + r);
result[cellKey] = cell;
}
}
return result;
}
}]);
}(_GoogleDriveAPI2["default"]);
var _default = exports["default"] = GoogleSheetAPI;