将Javascript变量传递给Google Script函数(Google Sheets)

Pass Javascript variable to Google Script function (Google Sheets)

本文关键字:Google Script 函数 Sheets Javascript 变量      更新时间:2023-09-26

我有一个JavaScript函数,它正在获取src值

<div>
  <img src="theUrl" id="imgID" onclick="myImageUrl()" />
</div>
<script>
  function myImageUrl() {
    var q = document.getElementById(imgID).src;
      google.script.run
       .imageCell();
  }
</script>

我似乎无法弄清楚如何将其传递到 Google 应用程序函数中,以便我可以将图像 URL 实现到单元格中,例如

function imageCell() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var range = ss.getActiveRange();
  range.setValue(q);
}

更新:

谢谢 Amit,但除非我错过了一些东西,否则不允许将该值用作保存的 var。我以两种方式运行它

.HTML

 <div id="theImageUrl">
    <img src="http://placekitten.com/100/100" id="theImageUrl" onclick="getImage()" />      
    </div>
    <script>
        function getImage() {
            q = document.getElementById( 'theImageUrl ).src;
            google.script.run.imageCell(q);
            }
        </script>
由我的小提琴

小提琴验证

服务器端

function imageCell(q) {
   var ss = SpreadsheetApp.getActiveSpreadsheet();
 var range = ss.getActiveRange();
range.setValue(q);
}

*.setValue(q) 和 .setValue();

在 HTML 端:

google.script.run.imageCell(q);

在服务器端:

function imageCell(q) { ...