我對 Google Scripts 和 HTML 還很陌生,我已經設法從其他帖子中收集到的東西放在一起,但是在嘗試從 Google Sheet 作業簿自動填充 html 下拉選單時,我碰壁了。
目的是在 Google 表格上彈出一個側邊欄,其中包含基于來自同一 Google 表格的資料的下拉選單。
在代碼和其他背景關系下方。
谷歌腳本
function mediaplanAdminSidebar() {
var widget = HtmlService.createHtmlOutputFromFile('mediaplan').setTitle('HELP');
SpreadsheetApp.getUi().showSidebar(widget)
SpreadsheetApp.newTextStyle().setBold(true)
}
代碼
<html>
<body>
<style>span{font-size: 34px;font-weight: bold;text-decoration: normal;font-style: normal;font-family: sans-serif;}</style>
<style>text{font-size: 24px;font-weight: normal;text-decoration: normal;font-style: normal;font-family: sans-serif;}</style>
<style>p{font-size: 14px;font-weight: normal;text-decoration: normal;font-style: normal;font-family: sans-serif;}</style>
<style>label{font-size: 14px;font-weight: normal;text-decoration: normal;font-style: normal;font-family: sans-serif;}</style>
<br/><span>Paid Social</span>
<br/><text>Media planner</text>
<p>Through this menu you will be able to produce a slide deck containing basic media buying info as well as the forecast for a campaign of your choice.</p>
<label>Select your campaign:</label></br>
<div>
<select>
<option>
<script>
var sheet = SpreadsheetApp.openById("SPREADSHEETID").getSheetByName('[PAID] #1-Activity tracker');
var myrange = sheet.getRange(7,6,1000);
var campnames = myrange.getValues();
campnames[1];
</script>
</option>
</select>
</div>
</body>
</html>
進一步的背景:
這對你們大多數人來說可能看起來非常錯誤,但我正在努力掌握它的基礎知識,而且我無法理解如何讓這兩段代碼相互交流。
我嘗試了幾種不同的方法,但都沒有成功。以上是我得到的最后一個。
該特定作業表中的給定范圍(7,6,10000)具有資料。
這就是它的樣子(見下文)。注意下拉選單是空的。

uj5u.com熱心網友回復:
當我看到您的腳本時,您似乎正在嘗試在 Javascript 端運行 Google Apps 腳本。我認為這是您的問題的原因。而且,當campnames[1];輸出時,使用陣列的第二個元素campnames。我認為這可能是您的第二個問題。
如果您想從 Google Apps Script 端的電子表格中檢索值并在 HTML 端的下拉串列中顯示值,那么下面的修改如何?
HTML 方面:
從:
<div>
<select>
<option>
<script>
var sheet = SpreadsheetApp.openById("SPREADSHEETID").getSheetByName('[PAID] #1-Activity tracker');
var myrange = sheet.getRange(7,6,1000);
var campnames = myrange.getValues();
campnames[1];
</script>
</option>
</select>
</div>
至:
<div><select><?!= data ?></select></div>
Google Apps 腳本方面:
從:
function mediaplanAdminSidebar() {
var widget = HtmlService.createHtmlOutputFromFile('mediaplan').setTitle('HELP');
SpreadsheetApp.getUi().showSidebar(widget)
SpreadsheetApp.newTextStyle().setBold(true)
}
至:
function mediaplanAdminSidebar() {
var sheet = SpreadsheetApp.openById("SPREADSHEETID").getSheetByName('[PAID] #1-Activity tracker');
var myrange = sheet.getRange(7, 6, 1000);
var campnames = myrange.getValues();
var widget = HtmlService.createTemplateFromFile('mediaplan');
widget.data = campnames.reduce((s, [e]) => s = `<option value="${e}">${e}<\/option>\n`, "");
SpreadsheetApp.getUi().showSidebar(widget.evaluate().setTitle('HELP'));
}
筆記:
- 關于選項的最大數量,我認為這個執行緒可能有用。參考
參考:
- HTML 服務:模板化 HTML
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/486502.html
標籤:javascript html 谷歌应用脚本 谷歌表格
上一篇:比較兩個串列并列印缺少的內容
