google-apps-script - 通过 AppScript 将 Facebook 广告帐户数据放入下拉选项列表
问题描述
我已经通过这个函数 makeRequest 成功检索了 Ad Accounts 数据。我可以在 appsscript 执行日志中看到结果 -
console.log(data.adaccounts.data[0].account_id); //26123718
我已经将数据存储在这里
常量数据 = JSON.parse(响应);
如何将广告帐户 ID 提取到下拉列表中,如 HTML 中所示
<select id="select">
</select>
function makeRequest() {
// get the Facebook Service
const facebookService = getFacebookService();
// get the access token
const access_token = facebookService.getAccessToken();
console.log(access_token);
//set up the API Endpoint
const base = 'https://graph.facebook.com/v10.0/';
const endpoint = 'me?&fields=adaccounts';
const url = base + endpoint;
console.log(url);
const params = {
headers: {
Authorization: 'Bearer ' + access_token
}
}
const response = UrlFetchApp.fetch(url,params);
console.log(response);
const data = JSON.parse(response);
console.log(data);
console.log(data.adaccounts.data[0].account_id);
}
解决方案
在您的 html 文件中,您可以使用google.script.run.withSuccessHandler()
调用 Code.gs 中的函数,如果该函数成功返回,它会将数据传递给回调函数。
例子:
代码.gs
function doGet() {
return HtmlService.createTemplateFromFile('Index').evaluate();
}
//GET DATA FROM GOOGLE SHEET AND RETURN AS AN ARRAY
function makeRequest(){
//Insert your function that will fetch data from facebook ads here
//For example the account IDs are "12345","32456", "23423"
var account_id = ["12345","32456", "23423"];
return account_id;
}
//INCLUDE JAVASCRIPT AND CSS FILES
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
索引.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<?!= include('JavaScript'); ?>
</head>
<body>
<div class="container">
<br>
<select id="select">
</select>
</div>
</body>
</html>
JavaScript.html
<script>
// This will call makeRequest() function in the Code.gs and
// pass the data to appendToDropDown function
google.script.run.withSuccessHandler(appendToDropDown).makeRequest();
//This function will add option to select tag
function appendToDropDown(ids){
var select = document.getElementById('select');
for (var i=0; i<ids.length; i++) {
var opt = document.createElement('option');
opt.text = ids[i]
select.add(opt);
}
}
</script>
输出:
参考:
推荐阅读
- lucene - Alfresco 服务器在 Solr4 alfresco-5.2.0 d 版本中抛出 Connection Refused Error 以及 lucene 解析器异常
- r - 如何使我的函数导入并连接/合并文件夹中的“所有”文件?
- java - 在 java 和 python 中创建对象时会发生什么
- sql - 日期范围excel VBA之间的strSQL
- javascript - 在其他地方更新状态时未触发 useEffect 挂钩
- php - 在 Gmail API 上使用刷新令牌。跟踪旧密钥
- meteor - 如何将mergeMap内部订阅限制为N个最新或滑动窗口队列
- python - 如何将 DatetimeIndexResamplerGroupby 对象转换为数据框?
- derby - Derby.properties 中的 derby.stream.error.rollingFile.limit 属性不起作用,因为翻转未按预期工作
- json - 解析json python的高效最快方法