英文:
Populate data from an API call in a drop-down with a corresponding account id
问题
I am extracting data from the GTM API to google sheets.
I have successfully achieved the API call to fetch all GTM accounts belonging to the user (On the front-end & server side), however, I am struggling with the API call to fetch a specific container for the selected account on the HTML side.
What I want to achieve:
So when the user selects an account, I want to populate the container name for that particular account selected in a drop-down. I am not sure how to call this function listContainers(accountId)
function and populate the drop-down.
html
//API call to fetch all GTM accounts belonging to the user
<label for="accountData">Select Account:</label>
<select id="accountData" style="width: 100%">
var data = listAccounts() ?>
for (i=0; i
<option value="<?= data[i].accountId ?>"><?= data[i].name ?></option>
} ?>
Code.gs
//Console.log
[ { accountId: 'xxxxx', name: 'ABC' },
{ accountId: 'xxxx', name: 'CBA' }]
function listAccounts() {
// API call to fetch all GTM accounts belonging to a user
return TagManager.Accounts.list({
fields: 'account(accountId,name)'
}).account;
}
// API call to fetch specific container for an account
//Console.log
[ { name: 'acb.net', containerId: 'XXXX' },
{ name: 'DEF', containerId: 'XXXX' } ]
function listContainers(accountId) {
// API call to fetch all containers belonging to a specific GTM account
return TagManager.Accounts.Containers.list(
'accounts/' + accountId,
{fields: 'container(name,publicId)'}
).container;
}
英文:
I am extracting data from the GTM API to google sheets.
I have successfully achieved the API call to fetch all GTM accounts belonging to the user (On the front-end & server side), however, I am struggling with the API call to fetch a specific container for the selected account on the HTML side.
What I want to achieve:
So when the user selects an account, I want to populate the container name for that particular account selected in a drop-down. I am not sure how to call this function listContainers(accountId)
function and populate the drop-down.
html
//API call to fetch all GTM accounts belonging to the user
<div class="form-group">
<label for="accountData">Select Account:</label>
<select id="accountData" style="width: 100%">
<? var data = listAccounts() ?>
<? for (i=0; i<data.length; i++) { ?>
<option value="<?= data)[i].accountId ?>" ><?= data)[i].name ?></option>
<? } ?>
</optgroup>
</select>
</div>
Code.gs
//**Console.log**
[ { accountId: 'xxxxx', name: 'ABC' },
{ accountId: 'xxxx', name: 'CBA' }]
function listAccounts() {
// API call to fetch all GTM accounts belonging to a user
return TagManager.Accounts.list({
fields: 'account(accountId,name)'
}).account;
}
// API call to fetch specific container for an account
//**Console.log**
[ { name: 'acb.net', containerId: 'XXXX' },
{ name: 'DEF', containerId: 'XXXX' } ]
function listContainers(accountId) {
// API call to fetch all containers belonging to a specific GTM account
return TagManager.Accounts.Containers.list(
'accounts/' + accountId,
{fields: 'container(name,publicId)'}
).container;
}
答案1
得分: 1
以下是翻译好的部分:
"我相信你的目标如下。
- 你有2个下拉列表。在你的问题中,你没有添加第2个下拉列表。
- 你想将
listAccounts()
的值设置为第一个下拉列表。 - 当选择第一个下拉列表时,你想将
listContainers(accountId)
的值设置为第二个下拉列表。
修改点:
- 如果你显示的脚本和HTML是实际情况,不幸的是,需要修改你的HTML模板。例如,
<?= data)[i].accountId ?>
,<?= data)[i].name ?>
中的)
。 - 在你的Google Apps Script中,我无法知道用于加载HTML模板的脚本。
- 从你的回复中,我理解你的
listAccounts()
和listContainers(accountId)
函数运行正常。
从这种情况来看,以下示例脚本如何?
HTML和Javascript部分:
请按照以下方式设置你的HTML模板。在这种情况下,你的Google Apps Script不需要修改。
<div class="form-group">
<label for="accountData">Select Account:</label>
<select id="accountData" style="width: 100%" onchange="select1(this)">
<? var data = listAccounts() ?>
<? for (i=0; i<data.length; i++) { ?>
<option value="<?= data[i].accountId ?>" ><?= data[i].name ?></option>
<? } ?>
</select>
<label for="accountData2">Select Account2:</label>
<select id="accountData2" style="width: 100%" onchange="select2(this)"></select>
</div>
<script>
function select1(e) {
google.script.run.withSuccessHandler(ar => {
const select = document.getElementById("accountData2");
select.innerHTML = "";
if (ar.length == 0) return;
ar.forEach(({name}) => {
const option = document.createElement("option");
option.text = name;
option.value = name;
select.appendChild(option);
});
}).listContainers(e.value);
}
function select2(e) {
alert(`${e.value} was selected.`);
}
</script>
- 在这个HTML和Javascript中,当加载HTML模板时,第一个下拉列表是由
listAccounts()
的值创建的。当从第一个下拉列表中选择一个值时,第二个下拉列表是由所选值和listContainers(accountId)
的值创建的。
注意:
- 我无法知道你用于加载HTML模板的脚本。因此,请确保你的Google Apps Script正常运行。请注意这一点。
参考:
英文:
I believe your goal is as follows.
- You have 2 dropdown lists. In your question, you didn't add the 2nd dropdown list.
- You want to set the values from
listAccounts()
to the 1st dropdown list. - When the 1st dropdown list is selected, you want to set the values from
listContainers(accountId)
to the 2nd dropdown list.
Modification points:
- If your showing script and HTML are the actual situation, unfortunately, your HTML template is required to be modified. For example, those are
)
of<?= data)[i].accountId ?>
,<?= data)[i].name ?>
. - In your Google Apps Script, I cannot know your script for loading the HTML template.
- From your reply, I understood that your functions of
listAccounts()
andlistContainers(accountId)
work fine.
From this situation, how about the following sample script?
HTML & Javascript side:
Please set your HTML template as follows. In this case, your Google Apps Script is not modified.
<div class="form-group">
<label for="accountData">Select Account:</label>
<select id="accountData" style="width: 100%" onchange="select1(this)">
<? var data = listAccounts() ?>
<? for (i=0; i<data.length; i++) { ?>
<option value="<?= data[i].accountId ?>" ><?= data[i].name ?></option>
<? } ?>
</select>
<label for="accountData2">Select Account2:</label>
<select id="accountData2" style="width: 100%" onchange="select2(this)"></select>
</div>
<script>
function select1(e) {
google.script.run.withSuccessHandler(ar => {
const select = document.getElementById("accountData2");
select.innerHTML = "";
if (ar.length == 0) return;
ar.forEach(({name}) => {
const option = document.createElement("option");
option.text = name;
option.value = name;
select.appendChild(option);
});
}).listContainers(e.value);
}
function select2(e) {
alert(`${e.value} was selected.`);
}
</script>
- In this HTML & Javascript, when the HTML template is loaded, the 1st dropdown list is created by the value of
listAccounts()
. And, when a value is selected from the 1st dropdown list, the 2nd dropdown list is created by the selected value and the value oflistContainers(accountId)
.
Note:
- I cannot know your script for loading the HTML template. So, I suppose that your Google Apps Script works fine. Please be careful about this.
Reference:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论