可以检索在Electron 表格的单元格中输入的信息. 接下来,如何将以HTML格式获得的信息作为初始值插入到文本字段中? 到目前为止,我已经创建了它,但我不能在初始值中动态插入值.

function onRemoveOrEditButton() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const initialValue = sheet.getRange("A1").getValue();
  var htmlOutput = HtmlService.createHtmlOutputFromFile('Dialog').setWidth(300);
  htmlOutput.append('<script>var initialValue = ' + JSON.stringify(initialValue) + ';</script>');
  SpreadsheetApp.getUi().showModalDialog(htmlOutput, ' ');
}
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <link href="https://unpkg.com/@primer/css@^20.2.4/dist/primer.css" rel="stylesheet"/>
    <style>
      .form-control {
        width: 200px;
      }
    </style>
  </head>
  <body>
      <input class="form-control input-block" type="text" id="name" name="name" placeholder="name" style="width: 300px;" value= "<?= initialValue ?>"><br>

  </body>
</html>

推荐答案

您的预期结果是否如下?

Google Apps脚本:

function onRemoveOrEditButton() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const initialValue = sheet.getRange("A1").getValue();
  var html = HtmlService.createTemplateFromFile('Dialog');
  html.initialValue = JSON.stringify(initialValue);
  html.sampleScript = `<script>var initialValue = ${JSON.stringify(initialValue)};console.log(initialValue);</script>`;
  var htmlOutput = html.evaluate().setWidth(300);
  SpreadsheetApp.getUi().showModalDialog(htmlOutput, ' ');
}

Html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <link href="https://unpkg.com/@primer/css@^20.2.4/dist/primer.css" rel="stylesheet"/>
    <style>
      .form-control {
        width: 200px;
      }
    </style>
  </head>
  <body>
      <input class="form-control input-block" type="text" id="name" name="name" placeholder="name" style="width: 300px;" value= <?!= initialValue ?>><br>
      <?!= sampleScript ?>
  </body>
</html>
  • 当您运行函数onRemoveOrEditButton时,会在Electron 表格上打开一个对话框.活动工作表的单元格值"A1"显示在输入标记中.此外,您还可以在控制台中看到值initialValue.

参考资料:

Javascript相关问答推荐

如何使用React渲染器放置根dis?

Flisk和JS错误:未捕获的Syntax错误:意外的令牌'<'

布局样式在刷新时不持续

如何解决这个未能在响应上执行json:body stream已读问题?

如何在RTK上设置轮询,每24小时

MathJax可以导入本地HTML文档使用的JS文件吗?

Angular 订阅部分相互依赖并返回数组多个异步Http调用

在开发期间,Web浏览器如何运行&qot;.jsx&qot;文件?

为什么在函数中添加粒子的速率大于删除粒子的速率?

使每个<;li>;元素的 colored颜色 与随机生成的 colored颜色 列表不同(不重复

FileReader()不能处理Firefox和GiB文件

React Refs不与高阶组件(HOC)中的动态生成组件一起工作

在HTML5画布上下文中使用putImageData时,重载解析失败

如何正确地在ComponentWillUnmount中卸载状态以避免内存泄漏?

在每次重新加载页面时更改指针光标

无法向甜甜圈图表上的ChartJSImage添加可见标签

在执行console.log(new X())时记录一个字符串

在AgGrid中显示分组行的单元格值

为什么我的InDesign Java脚本不能完全工作?

使用JavaScript或PHP从div ID值创建锚标记和链接