Google App脚本中的全局变量问题 [英] Issues with global variables in Google App Script

查看:88
本文介绍了Google App脚本中的全局变量问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试接收一些发送到Webhook的数据并使用它.我能够接收数据并将其转换为json字符串,但是我想将json字符串分配给全局变量,以便可以在其他地方和其他函数中使用它.

I'm trying to receive some data being sent to a webhook and work with it. I'm able to receive the data and convert it to a json String however I want to assign the json String to a global variable so I can use it elsewhere, and in other functions.

我先声明该变量,然后在收到它时尝试为其分配json字符串,但它似乎不起作用-该变量仍为'undefined'

I'm declaring the variable first and then trying to assign the json string to it when i receive it but it doesn't seem to be working - the variable is still 'undefined'

var jsonData;

function doPost(e){
    try{
        var jsonString = e.postData.getDataAsString();
        setLog("***json String = " + jsonString + " ***");
        jsonData = JSON.parse(jsonString);
    }
    catch(e){           
        setLog("***Exception occured = "+JSON.stringify(e) + " ***");  
    }
}

我是Java语言的新手,所以我不确定在哪里出错.

I'm quite new to Javascript and I'm not sure where I'm going wrong with this.

在其他地方查看时,我发现只要脚本像Google Apps脚本一样运行,全局变量就会更改,但是我不需要这些变量在每次脚本运行后都保持不变.我只需要全局变量在其他函数中使用即可.

When looking elsewhere I've found that global variables will be changed whenever the script runs as it's Google Apps Script, but I don't need these variables to remain the same after each time the script runs. I just need the global variable to use within other functions.

推荐答案

您要在其他功能上使用jsonData.我认为您的情况有3种模式.

You want to use jsonData at other functions. I think that there are 3 patterns for your situation.

这是一个简单的解决方案.您在doPost(e)中重新声明jsonData.由此,发生undefined.因此,进行如下修改.在这种情况下,jsonData将在运行的脚本完成后清除.

This is a simple solution. You redeclare jsonData in doPost(e). By this, undefined occurs. So modify as follows. In this case, jsonData is cleared after the running script was finished.

var jsonString = e.postData.getDataAsString();

到 :

jsonString = e.postData.getDataAsString(); // Remove "var"

模式2

这是一个解决方案,即使正在运行的脚本完成并再次运行,也可以使用jsonData.但是有时间限制.在这种情况下,它将使用缓存服务.使用缓存服务时,该值必须为字符串数据.

Pattern 2

This is a solution that even if the running script is finished and it is run again, jsonData can be used. But there is a time limit. In this case, it uses Cache Service. When Cache Service is used, the value is required to be the string data.

function doPost(e){
    try{
        var jsonString = e.postData.getDataAsString();
        setLog("***json String = " + jsonString + " ***");
//        jsonData = JSON.parse(jsonString);
        var cache = CacheService.getScriptCache();
        cache.put('jsonString', jsonString, 1500); // cache for 25 minutes
    }
    catch(e){           
        setLog("***Exception occured = "+JSON.stringify(e) + " ***");  
    }
}

// When you use jsonData, please JSON.parse() like this function.
function myFunction() {
  var cache = CacheService.getScriptCache();
  var jsonString = cache.get('jsonString');
  var jsonData = JSON.parse(jsonString);

  // do something
}

模式3

这是一个解决方案,即使正在运行的脚本完成并再次运行,也可以使用jsonData.在这种情况下,它将使用属性服务.使用属性服务"时,该值必须为字符串数据,并且没有时间限制.

Pattern 3

This is a solution that even if the running script is finished and it is run again, jsonData can be used. In this case, it uses Properties Service. When Properties Service is used, the value is required to be the string data and there is no time limit.

function doPost(e){
    try{
        var jsonString = e.postData.getDataAsString();
        setLog("***json String = " + jsonString + " ***");
//        jsonData = JSON.parse(jsonString);
        var scriptProperties = PropertiesService.getScriptProperties();
        scriptProperties.setProperty('jsonString', jsonString); // Save jsonString to the property.
    }
    catch(e){           
        setLog("***Exception occured = "+JSON.stringify(e) + " ***");  
    }
}

// When you use jsonData, please JSON.parse() like this function.
function myFunction() {
  var scriptProperties = PropertiesService.getScriptProperties();
  var jsonString = scriptProperties.getProperty('jsonString');
  var jsonData = JSON.parse(jsonString);

  // do something
}

如果我误解了你的情况,对不起.

If I misunderstand your situation, I'm sorry.

这篇关于Google App脚本中的全局变量问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆