I'm trying to use ES6 in a Google Spreadsheet (in the script.google.com part). I'm pretty new to JavaScript and maybe the error is trivial ...
- 28/09: The error for the post as changed as I was just using a Google Apps Script library name (Logger), I switch to
SomeClass
. I'm looking to module as my declaration is not the good one
What I have done:
- Created a webpack project
- Created a Logger class
- Created a main.js where I import the Logger class
- WebPack generate a bundle from my main.js
- I copy/paste the bundle.js in a bundle file on script.google
- I try to run a test in script.google but got
ReferenceError:
SomeClass is not define.`
Here is my code:
SomeClass.js
export default class SomeClass {
constructor() {
this.loggerSheet = SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName("ImportLog");
}
LogInfo(data) {
Logger.log(data);
loggerSheet.appendRow([new Date(), "INFO", data]);
}
}
Main.js
import SomeClass from './SomeClass.js';
Test in script.google
function test_bundle() {
var someClass = new SomeClass(); //<== breaks here
}
Bundle.js => copy/paste to script.google
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _SomeClassJs = __webpack_require__(4);
var _SomeClassJs2 = _interopRequireDefault(_SomeClassJs);
/***/ },
/* 1 */,
/* 2 */,
/* 3 */,
/* 4 */
/***/ function(module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var SomeClass = (function () {
function SomeClass(option) {
_classCallCheck(this, SomeClass);
this.loggerSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ImportLog");
}
_createClass(SomeClass, [{
key: "logInfo",
value: function logInfo(data) {
loggerSheet.appendRow([new Date(), "INFO", data]);
}
}]);
return SomeClass;
})();
exports["default"] = SomeClass;
module.exports = exports["default"];
/***/ }
/******/ ]);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…