[英]pass google script object to html template
我使用HtmlService在Google腳本中處理獨立的Web應用程序。 這是代碼的一部分:
腳本:
function doGet() {
getLabelsLists();
return HtmlService
.createTemplateFromFile('index')
.evaluate().setSandboxMode(HtmlService.SandboxMode.NATIVE);
}
function getEmails() {
var query = ...;
var threads = GmailApp.search (query);
return threads;
}
index.html的:
<? var data = getEmails(); ?>
<table>
<? for (var i = 0; i < data.length; i++) {
var message = data[i].getMessages()[0];
?>
<tr onclick= click(<?= message.getId() ?>)>
<td><?= message.getSubject() ?></td>
</tr>
<? } ?>
</table>
我想做的是僅在頁面加載和單擊調用函數中顯示如下所示的按鈕,該按鈕將結果顯示在表格中。
<input type="button" onclick="google.script.run.getEmails()" />
有什么想法可以將Gmail線程數組從getEmails()函數傳遞到視圖並動態創建表嗎?
您不能傳遞此類對象(帶有函數和自定義原型),只能傳遞“簡單”對象(由字符串,數字,布爾值和數組或包含這些對象的對象組成),如文檔中所述 。 因此,您將使用需要傳遞的值來構建一個對象。 以您的使用示例為例,該示例僅獲取線程中第一條消息的ID和主題,這是如何完成的:
function getEmails() {
var query = "...";
return GmailApp.search(query).map(function(thread) {
var msg = thread.getMessages()[0];
return {id:msg.getId(), subject:msg.getSubject()};
}
}
然后,要“讀取”值,應該直接訪問id
,而不是調用getId()
和subject
,而不是getSubject()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.