本文介绍了在GAS项目下跨多个脚本文件访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的项目:

TestProject

-代码gs

-14885.gs

-23546.gs

我想创建一个变量并将其值设置在"Code.gs"文件中.然后,我应该可以在14885.gs和23456.gs文件中访问它.

I want to create a variable and set its value in "Code.gs" file. Then I should be able to access it in 14885.gs and 23456.gs files.

在Code.gs文件中,我使用了PropertiesServices并创建了一个我想在项目下的其他"gs"文件之间访问的变量.

In Code.gs file, I used the PropertiesServices and created a variable that I wanted to access across other "gs" files under the project.

在Code.gs文件中

In Code.gs file

function onEdit(e){  
   totalHoursLoggedForStory_today =  Number(e.range.getSheet().getRange("H3").getValue()) + Number(e.range.getSheet().getRange("H4").getValue()) + Number(e.range.getSheet().getRange("H5").getValue()); 

  var userProperties = PropertiesService.getUserProperties();
  var newProperties = {'hoursLogged': totalHoursLoggedForStory_today };
  userProperties.setProperties(newProperties); 

  onEditOfH3(e);
} 

在23546.gs文件中

In 23546.gs file

  function onEditOfH3(e){   
   if (e.range.getA1Notation() == "H3") 
    { 
    var temp= userProperties.getProperty('hoursLogged'); 
    sh.alert(temp);
    }

预期:在23546.gs文件中,我应该能够警告在"Code.gs"文件中设置的属性.

Expected: In the 23546.gs file, I should be able to alert the property that I set in "Code.gs" file.

实际:我看到此错误:ReferenceError:未定义userProperties

Actual: I am seeing this error: ReferenceError: userProperties is not defined

推荐答案

  • 要在运行onEdit时将userProperties与在onEditOfH3函数中在onEdit函数中给出的值一起使用.
  • 您想了解一个GAS项目中文件的规范.
    • You want to use userProperties with the value, which is given in the function of onEdit, at the function of onEditOfH3, when onEdit is run.
    • You want to understand about the specification of the files in one GAS project.
    • 如果我的理解是正确的,那么这个答案如何?请认为这只是几个答案之一.

      If my understanding is correct, how about this answer? Please think of this as just one of several answers.

      在Google Apps脚本的项目中,该项目中的所有文件都被用作一个项目.即,例如,当在Code.gs文件中运行以下示例脚本时,

      At the project of Google Apps Script, all files in the project are used as one project. Namely, for example, when the following sample script is run in the file of Code.gs,

      function myFunction() {
        for (var i in this) {
          if (typeof this[i] == "function") {
            Logger.log(i)
          }
        }
      }
      

      返回项目中所有文件中的所有函数.由此发现,在Code.gs文件中声明全局变量时,可以在同一项目中的其他文件中使用此变量.在以下模式1中,将使用此模式.

      all functions in all files in the project are returned. From this, it is found that when a global variable is declared at the file of Code.gs, this variable can be used at other file in the same project. At the following pattern 1, this is used.

      在此模式下,userProperties被声明为全局变量.

      In this pattern, userProperties is declared as the global variable.

      var userProperties; // This is declared as the global variable.
      
      function onEdit(e){
        totalHoursLoggedForStory_today =  Number(e.range.getSheet().getRange("H3").getValue()) + Number(e.range.getSheet().getRange("H4").getValue()) + Number(e.range.getSheet().getRange("H5").getValue());
      
        userProperties = PropertiesService.getUserProperties(); // Modified
        var newProperties = {'hoursLogged': totalHoursLoggedForStory_today };
        userProperties.setProperties(newProperties); 
      
        onEditOfH3(e);
      }
      

      23546克

      不需要对此进行修改.

      23546.gs

      This is not required to be modified.

      在此模式中,将userProperties添加到e的对象.该值在onEditOfH3函数中用作e.userProperties.如果您不想使用全局变量,该怎么办?另外,userProperties可以作为另一个参数发送.

      In this pattern, userProperties is added to the object of e. And the value is used as e.userProperties in the function of onEditOfH3. If you don't want to use the global variable, how about this? Also, userProperties can be sent as another argument.

      function onEdit(e){
        totalHoursLoggedForStory_today =  Number(e.range.getSheet().getRange("H3").getValue()) + Number(e.range.getSheet().getRange("H4").getValue()) + Number(e.range.getSheet().getRange("H5").getValue());
      
        var userProperties = PropertiesService.getUserProperties();
        var newProperties = {'hoursLogged': totalHoursLoggedForStory_today };
        userProperties.setProperties(newProperties);
        e.userProperties = userProperties; // Added
        onEditOfH3(e);
      } 
      

      23546克

      function onEditOfH3(e){
        // var sh = SpreadsheetApp.getUi(); // In your whole script, this might be declared at elsewhere.
        if (e.range.getA1Notation() == "H3") { 
          var temp = e.userProperties.getProperty('hoursLogged'); // Modified
          sh.alert(temp);
        }
      }
      

      参考:

      • 脚本项目
      • Reference:

        • Script Projects
        • 这篇关于在GAS项目下跨多个脚本文件访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 03:44