我在这里遇到了什么。
我正在加载一个HTML模板,在这个页面中有很多JavaScript正在进行。
我正试图通过使用我的Google表格的onOpen()
缓存模板来加速操作。我无法计算如何缓存我的HTML页面CalForm.html
(来自我的内部Google Sheet脚本)。
这就是我现在所拥有的:
function CacheCreate() {
CacheService.getScriptCache().put('CalCache', 'CalForm');
Browser.msgBox("done");
}
var evalSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Evaluation');
var row = evalSheet.getActiveCell().getRow();
var CalCache2 = CacheService.getScriptCache().get('CalCache');
Browser.msgBox(CacheService.getScriptCache().get('CalCache'))
initialize(row);
//var cache = CacheService.getScriptCache();
//var cache2 = cache.get('rss-feed-contents');
//Browser.msgBox(cache.get('rss-feed-contents'));
var html = HtmlService
.createTemplateFromFile(CalCache2)
.evaluate()
.setWidth(1200)
.setHeight(560)
.setSandboxMode(HtmlService.SandboxMode.NATIVE);
SpreadsheetApp.getUi().showModalDialog(html, 'Calculatrice');
感谢您的帮助!
答案 0 :(得分:0)
首先,您需要从“内部Google表格脚本”中获取HTML,以下代码将获取HTML(字符串格式),因为您的脚本中有一个名为“template.html”的文件。
var template = HtmlService.createHtmlOutputFromFile('template').getContent();
我检查数据是否已经在缓存中......
function getObjectsFromCache(key, objects, flush)
{
Logger.log("Running: getObjectsFromCache(" + key + ", objects)");
var cache = CacheService.getScriptCache();
var cached = cache.get(key);
flush = false; // 1st run 45.33, 2nd run 46.475
//flush = true;// 65.818 seconds
if (cached != null && flush != true)
{
Logger.log("\tEXISTING DATA -> ");
cached = cache.get(key);
}
else
{
Logger.log("\tNEW DATA -> ");
//Logger.log("\tJSON.stringify(objects): " + JSON.stringify(objects) + ", length: " + objects.length);
//If you're working with spreadsheet objects, or array data, you'll want to put the data in the cache as a string, and then reformat the data to it's original format, when it is returned.
//cache.put(key, JSON.stringify(objects), 1500); // cache for 25 minutes
//In your case, the HTML does not need to be stringified.
cache.put(key, objects, 1500); // cache for 25 minutes
cached = objects;
}
return cached;
}
我注释掉了JSON.Stringify(对象),因为在我的原始代码中我使用另一个名为formatCachedData(cache,key)的函数来返回不同类型的数据 - 一个多维数组(电子表格数据),或者来自AdminDirectory.Users.list({...})的Google用户数据。