简体   繁体   English

为什么我会收到“ReferenceError:未定义测试”

[英]Why do I get “ReferenceError: test is not defined”

Within my Google Script Project I got two GS files Code.gs and other.gs .在我的谷歌脚本项目中,我得到了两个 GS 文件Code.gsother.gs

code.gs looks like code.gs 看起来像

var globalSettings = {};
settings();


function settings(){
  other();
  globalSettings.fileName = "file";
  console.log("settings was executed");
}

function primary(){

  console.log("primary was executed");
}

other.gs looks like other.gs 看起来像

function other(){

  console.log("other was executed");
}

when I run the function primary I get当我运行 function primary时,我得到

ReferenceError: other is not defined
settings    @ Code.gs:5
(anonymous) @ Code.gs:1

when I move the function other to the file code it works.当我将 function other移动到文件code时,它可以工作。 Could someone explain why?有人可以解释为什么吗? Is there any way the other file could be anywhere in the project?有没有办法让其他文件在项目中的任何地方?

Explanation:解释:

Everytime you call a function (in any script in your project), the global variables are automatically executed .每次调用 function(在项目中的任何脚本中)时,都会自动执行全局变量。

  • This is why if you define var globalSettings = {} as a global decleration, every time you run any function in the project, all the global calls will be executed and therefore globalSettings will be set to an empty object and this is why I don't use global variables.这就是为什么如果您将var globalSettings = {}定义为全局 decleration,每次您在项目中运行任何 function 时,都会执行所有全局调用,因此globalSettings将设置为空 object 这就是我不这样做的原因t 使用全局变量。

  • The global call other and the function decleration other need to be in the same gs script in order to work.全局调用otherfunction decleration other需要在同一个gs脚本中才能工作。 Or you could simply call other from within the functions settings or primary and in this way other can stay in a separate script.或者您可以简单地从功能settingsprimary函数中调用other ,这样other可以保留在单独的脚本中。

For example this would work perfectly fine:例如,这会很好地工作:

code.gs

// define global variables
var globalSettings = {};

// adjust global variables here as a helper function
function settings(){
  other();
  globalSettings.fileName = "file";
  console.log("settings was executed");
}

// main function to be executed
function primary(){
  settings(); // call settings
  console.log(globalSettings.fileName);
  console.log(globalSettings.date);
  console.log("primary was executed");
}

other.gs

// make additional adjustments to the global variables
function other(){
  globalSettings.date = "today";
  console.log("other was executed");
}

Suggestion:建议:

A better idea to make sure you don't execute your global declerations, is to use the Class PropertiesService class to store some script or user data and then you can retrieve them either globally or locally (inside the functions) and this will make sure you won't execute them accidentally upon every execution as it is the case for the global declerations.确保不执行全局声明的更好方法是使用 Class PropertiesService class 来存储一些脚本或用户数据,然后您可以全局或本地(在函数内部)检索它们,这将确保您不会像全局声明那样在每次执行时意外执行它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么会出现Uncaught ReferenceError:未定义scrollToM? - Why do I get Uncaught ReferenceError: scrollToM is not defined? 为什么会收到“未捕获的ReferenceError:未定义$(匿名函数)” - Why do I get an “Uncaught ReferenceError: $ is not defined (anonymous function)” 为什么我会收到此错误:“未捕获的 ReferenceError:未定义 value1”? - Why do I get this error: “Uncaught ReferenceError: value1 is not defined”? 为什么我不断收到ReferenceError:*未定义? - Why do I keep getting ReferenceError: * is not defined? 为什么会收到未捕获的ReferenceError:未定义$ - Why do I receive an Uncaught ReferenceError: $ is not defined 为什么收到“未捕获的ReferenceError:未定义要求”? - Why I get “Uncaught ReferenceError: require is not defined”? 为什么我会得到这个JavaScript ReferenceError - Why do I get this JavaScript ReferenceError 为什么我在单元测试中收到 `ReferenceError: jQuery is not defined` - Why i get `ReferenceError: jQuery is not defined` on unit testing 为什么这段代码抛出ReferenceError:test没有定义? - Why does this code throw ReferenceError: test is not defined? 为什么我得到 Uncaught ReferenceError: Delete is not defined at HTMLButtonElement.onclick 如果已经定义? - Why I get Uncaught ReferenceError: Delete is not defined at HTMLButtonElement.onclick IF IS already defined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM