英文:
how to save a map object as a global variable in appscript
问题
The code you provided seems to be an attempt to save a Map object in Google Apps Script using script properties. However, it appears that you are encountering issues with retrieving the Map correctly. Here's the translation of your code:
// 在 open 函数中
var scriptPrp = PropertiesService.getScriptProperties();
scriptPrp.setProperty('anglishWords', jsonText);
// 在向地图添加更多元素的函数中
// 获取 JSON 文本对象并将其重新转换为地图
var scriptPrp = PropertiesService.getScriptProperties();
var jsonT = scriptPrp.getProperty('anglishWords');
const anglishWordsE = new Map(Object.entries(JSON.parse(jsonT)));
It seems like you are trying to save the Map as a JSON string in script properties and then parse it back into a Map when you retrieve it. If you are getting an empty map, please make sure that jsonText
contains the serialized Map when you initially set it in the script properties. Additionally, ensure that you are using the correct property key when retrieving the data.
If you continue to face issues, you might want to check the contents of jsonText
and the process of serializing and deserializing the Map to see where the problem lies.
英文:
what is the most efficient method for saving a Map Object in app script with setting script properties. I have attempted to strigify the map and retrieve it, however it only returns an empty map. I have limited knowledge of app script and javascript and therefore might have made misunderstood some part of the language.
This is my current attempt with only returns an empty map
//In open function
var scriptPrp = PropertiesService.getScriptProperties();
scriptPrp.setProperty('anglishWords', jsonText);
//In function that adds more elements to the map
//grabbing the json text object and converting it into a map again
var scriptPrp = PropertiesService.getScriptProperties();
var jsonT = scriptPrp.getProperty('anglishWords');
const anglishWordsE = new Map(Object.entries(JSON.parse(jsonT)));
This method returns an empty map even if had values in the map that I saved and doesn't change after a set is added to the map
答案1
得分: 2
在PropertiesService
的情况下,可以存储字符串值。参考 很不幸,在当前阶段,不能直接存储Map
对象。如果您想实现这一点,需要将Map
对象转换为字符串。示例脚本如下。
示例脚本:
// 准备一个示例的Map对象。
const sampleMap = new Map([["key1", "value1"], ["key2", "value2"], ["key3", "value3"]]);
console.log(sampleMap.get("key2")); // <--- value2
// 将Map对象存储为字符串到PropertiesService。
const prop = PropertiesService.getScriptProperties();
prop.setProperty("sample", JSON.stringify([...sampleMap]));
// 从PropertiesService加载Map对象。
const str = prop.getProperty("sample");
const loadedMap = new Map(JSON.parse(str));
console.log(loadedMap.get("key2")); // <--- value2
注意:
- 顺便提一下,PropertiesService存在一些限制,请注意。参考(作者:我)
参考链接:
英文:
In the case of PropertiesService
, the string value can be stored. Ref So, unfortunately, in the current stage, the Map object cannot be directly stored. If you want to achieve this, it is required to convert the Map object to the string. The sample script is as follows.
Sample script:
// Prepare a sample Map object.
const sampleMap = new Map([["key1", "value1"], ["key2", "value2"], ["key3", "value3"]]);
console.log(sampleMap.get("key2")); // <--- value2
// Store the Map object as a string to PropertiesService.
const prop = PropertiesService.getScriptProperties();
prop.setProperty("sample", JSON.stringify([...sampleMap]));
// Load the Map object from PropertiesService.
const str = prop.getProperty("sample");
const loadedMap = new Map(JSON.parse(str));
console.log(loadedMap.get("key2")); // <--- value2
Note:
- By the way, there are the limitations in PropertiesService. Please be careful about this. Ref (Author: me)
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论