最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get data from localStorage (google script) - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 6

You 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.

发布评论

评论列表(0)

  1. 暂无评论