admin管理员组文章数量:1327524
In my app for google spreadsheet I save some params in localStorage in one function. And in another function I need to retrieve them. Of course, I can create modeless dialog or sidebar, when will be my html code and js, but this second function only refresh current sheet, so I don't need UI. Also I know, that server side doesn't have access to localStorage. Here is my abstract code:
server.gs
:
function menuItemClicked()
{
// when we click on item in addon menu, this called
// and load 'client.html'
}
function refreshCurrentSheet(params)
{
// called by 'client.html'
// do something else with params...
}
client.html
:
<script type="text/javascript">
google.script.run.refreshCurrentSheet( localStorage.getItem('var') );
</script>
I didn't find answer in API, how to do that(
Question: how can I load html and execute js without visible UI ?
P.S. Saving these params on server-side in cache is not a solution, because I often use and change them in client
In my app for google spreadsheet I save some params in localStorage in one function. And in another function I need to retrieve them. Of course, I can create modeless dialog or sidebar, when will be my html code and js, but this second function only refresh current sheet, so I don't need UI. Also I know, that server side doesn't have access to localStorage. Here is my abstract code:
server.gs
:
function menuItemClicked()
{
// when we click on item in addon menu, this called
// and load 'client.html'
}
function refreshCurrentSheet(params)
{
// called by 'client.html'
// do something else with params...
}
client.html
:
<script type="text/javascript">
google.script.run.refreshCurrentSheet( localStorage.getItem('var') );
</script>
I didn't find answer in API, how to do that(
Question: how can I load html and execute js without visible UI ?
P.S. Saving these params on server-side in cache is not a solution, because I often use and change them in client
Share Improve this question asked Jun 20, 2017 at 14:23 FLighterFLighter 4391 gold badge5 silver badges19 bronze badges 5- If you don't need the UI at all, why not use Properties Service to persist data? Perhaps I misunderstood your question – Anton Dementiev Commented Jun 20, 2017 at 16:03
- I believe Google Apps Script is based on Javascript 1.6. It has access to localstorage so my guess is so does Google Apps Script. If not someone will correct me hopefully. Here's a reference . But even if it didn't I don't see any disadvantage to using PropertyService you can access any script with google.script.run – Cooper Commented Jun 20, 2017 at 16:10
- localStorage belongs to the window object in the browser. GAS is based on JS, but it runs in a different environment on Google servers, which means the global object is different. – Anton Dementiev Commented Jun 20, 2017 at 17:32
- @AntonDementiev, really, I didn't know about Properties Service. If it doesn't have a time limit like Cache Service, it's the solution to the problem. Thank you – FLighter Commented Jun 21, 2017 at 7:07
- 1 I undeleted my original answer to you. If you'd like to store data for a long period of time, then Properties Service would be your best bet. The reason I remended Cache Service over Properties is because I thought you wanted to save data between multiple function calls. – Anton Dementiev Commented Jun 21, 2017 at 15:29
2 Answers
Reset to default 6You can use either Properties Service or Cache Service. Both provide localStorage functionality allowing you to store key value pairs (string type only). If you'd like to persist data between function calls, Cache is probably the best option
More on Cache Service https://developers.google./apps-script/reference/cache/
More on Properties Service https://developers.google./apps-script/reference/properties/
My points are too low to reply to the any posts. However, I tried both CacheServices and PropertiesService in a google apps scripts file, and none of these functions store variables in localStorage in the browser. Below is the code that I used to save the variables, and I confirmed the saved output on the google apps scripts side; it only works in the Google Apps script side and not for the browser.
// Way 0: Save a variable
var cache = CacheService.getScriptCache();
cache.put('test_key_name', sheet.getRange("A2").getValues());
// Way 0: Read a variable
var out = cache.get("test_key_name");
// Way 1: Save a variable
PropertiesService.getScriptProperties().setProperty('test_key_name', sheet.getRange("A2").getValues());
// Way 1: Read a variable
var out = PropertiesService.getScriptProperties().getProperty('test_key_name');
Regarding the browser-side, I can not find these variables in the browser storage area.
The information that the user provided is misleading and it would have been helpful to everyone if they provided a snippet of code with the url. I do not have any formal training on website development besides a few online classes, however the only way that I have found to transfer variables from the client-side to the server-side is to deploy the google apps script and use localStorage mands in the index.html file.
// Code.gs
function doGet() {
return HtmlService.createHtmlOutputFromFile('index');
}
function get_Spreadsheet_values() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
// Read a variable
var dictt = {};
dictt.variable0 = sheet.getRange("A2").getValues();
return dictt;
}
// index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<script>
// Get values from the spreadsheet : ONLY WORKS FOR THE SERVER
// Saving SpreadSheet values to localStorage
google.script.run.withSuccessHandler(function(result){ localStorage.setItem('variable0', result.variable0);}).get_Spreadsheet_values();
</script>
</body>
</html>
However, I do not think that this is a good solution for the client-side because most Google SpreadSheet users may not know how to deploy the spreadsheet to a webapp; the spreadsheet script should just work when you give it to the user.
本文标签: javascriptGet data from localStorage (google script)Stack Overflow
版权声明:本文标题:javascript - Get data from localStorage (google script) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742181395a2428529.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论