如何将一个地图对象保存为AppScript中的全局变量。

huangapple go评论51阅读模式
英文:

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

注意:

参考链接:

英文:

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:

huangapple
  • 本文由 发表于 2023年6月13日 04:43:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定