簡體   English   中英

從 JavaScript 下拉菜單更改 Google 表格單元格值

[英]Changing Google Sheets Cell Value from JavaScript Dropdown Menu

我正在嘗試根據用戶從下拉菜單中的選擇更改 Google 表格中特定單元格的值。 此外,可能值得一提的是,此下拉菜單位於從表格數據生成的 HTML 表中。 除了這件小事,一切正常。 幫助將不勝感激!

HTML 片段:

<select id="status" name="status">
    <option value="current" id="current"><?= tableData[i][j] ?></option>
    <option value="submitted" id="submitted">Submitted</option>
</select>
<script>
    let select = document.querySelector('#status');
    select.addEventListener('change', update(i, j));
</script>

代碼.gs function:

var rfi= SpreadsheetApp.openById("REDACTED");
var sheet = rfi.getSheetByName('Sheet1'); 

function update(i, j) {
    sheet.getRange(i, j).setValue("In Review");
}

如果你想傳遞坐標,試試

在 html

<option value="<?=(i+1)+'|'+(j+1)?>" id="current"><?= tableData[i][j] ?></option>

在 GS

function update(coord) {
  sheet.getRange(coord.split('|')[0], coord.split('|')[1]).setValue("In Review");
}

將在您的電子表格發布后完成

樣本

例如,這有效並顯示了如何傳輸坐標

html

<!DOCTYPE html>
<html>
<head>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
  <script>
    <? var data = listOfItems(); ?>
  </script>
    <select id="choice" name="choice">
      <option value="" disabled selected >Choose ...</option>
        <? for (var i = 0; i < data.length; i++) { ?>
          <? for (var j = 0; j < data[0].length; j++) { ?>
            <option value="<?=(i+1)+'|'+(j+1)?>" ><?=data[i][j]?></option>
          <? } ?>
        <? } ?>
    </select>
    <script>
      $('#choice').change(function(){ 
        google.script.run.update ($(this).val());
      });
    </script>
</body>

gs

function load() {
  var ui = HtmlService.createTemplateFromFile('load')
      .evaluate()
      .setSandboxMode(HtmlService.SandboxMode.IFRAME)
      .setTitle("test ...");
  SpreadsheetApp.getUi().showSidebar(ui);
}
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('mySheet')
function listOfItems() {
  return  sheet.getDataRange().getValues()
};
function update(coord){
  sheet.getRange(coord.split('|')[0], coord.split('|')[1]).setValue("In Review");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM