从API调用中填充下拉菜单,带有相应的账户ID。

huangapple go评论63阅读模式
英文:

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%">


<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

&lt;div class=&quot;form-group&quot;&gt;

     &lt;label for=&quot;accountData&quot;&gt;Select Account:&lt;/label&gt;
		  &lt;select id=&quot;accountData&quot; style=&quot;width: 100%&quot;&gt;

 &lt;? var data = listAccounts() ?&gt;

&lt;? for (i=0; i&lt;data.length; i++) { ?&gt;
    
    &lt;option value=&quot;&lt;?= data)[i].accountId ?&gt;&quot; &gt;&lt;?= data)[i].name ?&gt;&lt;/option&gt;
   &lt;? } ?&gt;
&lt;/optgroup&gt;
 
   
   &lt;/select&gt;
&lt;/div&gt;

Code.gs


//**Console.log**

[ { accountId: &#39;xxxxx&#39;, name: &#39;ABC&#39; },
  { accountId: &#39;xxxx&#39;, name: &#39;CBA&#39; }]

function listAccounts() {
  // API call to fetch all GTM accounts belonging to a user
  return TagManager.Accounts.list({
    fields: &#39;account(accountId,name)&#39;
  }).account;
}

// API call to fetch specific container for an account



//**Console.log**
	[ { name: &#39;acb.net&#39;, containerId: &#39;XXXX&#39; },
    { name: &#39;DEF&#39;, containerId: &#39;XXXX&#39; } ]


function listContainers(accountId) {
  // API call to fetch all containers belonging to a specific GTM account
  return TagManager.Accounts.Containers.list(
    &#39;accounts/&#39; + accountId,
    {fields: &#39;container(name,publicId)&#39;}
  ).container;
}


答案1

得分: 1

以下是翻译好的部分:

"我相信你的目标如下。

  • 你有2个下拉列表。在你的问题中,你没有添加第2个下拉列表。
  • 你想将listAccounts()的值设置为第一个下拉列表。
  • 当选择第一个下拉列表时,你想将listContainers(accountId)的值设置为第二个下拉列表。

修改点:

  • 如果你显示的脚本和HTML是实际情况,不幸的是,需要修改你的HTML模板。例如,&lt;?= data)[i].accountId ?&gt;&lt;?= data)[i].name ?&gt;中的
  • 在你的Google Apps Script中,我无法知道用于加载HTML模板的脚本。
  • 从你的回复中,我理解你的listAccounts()listContainers(accountId)函数运行正常。

从这种情况来看,以下示例脚本如何?

HTML和Javascript部分:

请按照以下方式设置你的HTML模板。在这种情况下,你的Google Apps Script不需要修改。

&lt;div class=&quot;form-group&quot;&gt;
  &lt;label for=&quot;accountData&quot;&gt;Select Account:&lt;/label&gt;
  &lt;select id=&quot;accountData&quot; style=&quot;width: 100%&quot; onchange=&quot;select1(this)&quot;&gt;
    &lt;? var data = listAccounts() ?&gt;
    &lt;? for (i=0; i&lt;data.length; i++) { ?&gt;
    &lt;option value=&quot;&lt;?= data[i].accountId ?&gt;&quot; &gt;&lt;?= data[i].name ?&gt;&lt;/option&gt;
    &lt;? } ?&gt;
  &lt;/select&gt;

  &lt;label for=&quot;accountData2&quot;&gt;Select Account2:&lt;/label&gt;
  &lt;select id=&quot;accountData2&quot; style=&quot;width: 100%&quot; onchange=&quot;select2(this)&quot;&gt;&lt;/select&gt;
&lt;/div&gt;

&lt;script&gt;
function select1(e) {
  google.script.run.withSuccessHandler(ar =&gt; {
    const select = document.getElementById(&quot;accountData2&quot;);
    select.innerHTML = &quot;&quot;;
    if (ar.length == 0) return;
    ar.forEach(({name}) =&gt; {
      const option = document.createElement(&quot;option&quot;);
      option.text = name;
      option.value = name;
      select.appendChild(option);
    });
  }).listContainers(e.value);
}

function select2(e) {
  alert(`${e.value} was selected.`);
}
&lt;/script&gt;
  • 在这个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 &lt;?= data)[i].accountId ?&gt;, &lt;?= data)[i].name ?&gt;.
  • 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() and listContainers(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.

&lt;div class=&quot;form-group&quot;&gt;
  &lt;label for=&quot;accountData&quot;&gt;Select Account:&lt;/label&gt;
  &lt;select id=&quot;accountData&quot; style=&quot;width: 100%&quot; onchange=&quot;select1(this)&quot;&gt;
    &lt;? var data = listAccounts() ?&gt;
    &lt;? for (i=0; i&lt;data.length; i++) { ?&gt;
    &lt;option value=&quot;&lt;?= data[i].accountId ?&gt;&quot; &gt;&lt;?= data[i].name ?&gt;&lt;/option&gt;
    &lt;? } ?&gt;
  &lt;/select&gt;

  &lt;label for=&quot;accountData2&quot;&gt;Select Account2:&lt;/label&gt;
  &lt;select id=&quot;accountData2&quot; style=&quot;width: 100%&quot; onchange=&quot;select2(this)&quot;&gt;&lt;/select&gt;
&lt;/div&gt;

&lt;script&gt;
function select1(e) {
  google.script.run.withSuccessHandler(ar =&gt; {
    const select = document.getElementById(&quot;accountData2&quot;);
    select.innerHTML = &quot;&quot;;
    if (ar.length == 0) return;
    ar.forEach(({name}) =&gt; {
      const option = document.createElement(&quot;option&quot;);
      option.text = name;
      option.value = name;
      select.appendChild(option);
    });
  }).listContainers(e.value);
}

function select2(e) {
  alert(`${e.value} was selected.`);
}
&lt;/script&gt;
  • 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 of listContainers(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:

huangapple
  • 本文由 发表于 2023年4月17日 15:06:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定