为什么只打印一个 属性

Why is only one property being printed

我正在使用 Google Apps 脚本开发一个项目,该项目使用 scriptProperties 服务来保存用户输入的文本,并在稍后打开程序时打印上次输入的内容。我 运行 遇到的问题是它只打印最近保存的值,而其他值没有按预期打印出来。只有最近保存的 属性.

这是我正在使用的东西。

这是 HTML 文档。

<html>
  <head>
    <base target="_top">
    <script>
      var inputs = {};

      <? var data = sendData(); ?> 

     function captureInputs() {
       google.script.run.storeData(inputs);
     }

     function title() {
       var inputTitle = document.getElementById("metaTitle").value;
       inputs.title = inputTitle;
      }

     function url() {
       var inputUrl = document.getElementById("url").value;
       inputs.url = inputUrl;
       }

     function desc() {
       var inputDesc = document.getElementById("metaDescription").value;
       inputs.desc = inputDesc;
       }
    </script>
  </head>
  <body>
    <? if (data.title !== "undefined") { ?>
        <input value="<?= data.title ?>" type="text" oninput="title();captureInputs();>
      <? } else { ?>
        <input value="" type="text" oninput="title();captureInputs();>
    <? } ?>

    <? if (data.url !== "undefined") { ?>
        <input value="<?= data.url ?>" type="text" oninput="url();captureInputs();>
      <? } else { ?>
        <input value="" type="text" oninput="url();captureInputs();>
    <? } ?>

    <? if (data.desc !== "undefined") { ?>
      <input value="<?= data.desc ?>" type="text" oninput="desc();captureInputs();>
    <? } else { ?>
      <input value="" type="text" oninput="desc();captureInputs();>
    <? } ?>

  </body>
</html>

这是我在 JS 文件中的内容。

function sendData() {
  var data = PropertiesService.getScriptProperties().getProperties();
  return data;
  }

function storeData(inputs) {
 var scriptProperties = PropertiesService.getScriptProperties();
 scriptProperties.setProperty('title', inputs.title);
 scriptProperties.setProperty('url', inputs.url);
 scriptProperties.setProperty('desc', inputs.desc);
 }

因此,例如,如果我将某些内容保存到 data.title,那么 data.desc 将显示为未定义(它 有时 显示为空白)即使我在那里保存了 属性。如果我重写 data.desc,那么 data.title 将显示为空。

知道为什么会发生这种情况吗?

PS。这显然不是完整的程序,但我将其缩短到我 运行 遇到问题的地方。

我对您的 html 进行了一些修改,因此我可以更改并保存数据,然后重新启动对话框以查看它是否收到了我的更改。

html:

<html>
  <head>
    <base target="_top">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

    <script>
      <? var data = sendData(); ?> 
      function saveData() {
       var obj={title:$('#title').val(),desc:$('#desc').val(),url:$('#url').val()};
       $('#title').val('');//set text to blank on the way to server
       $('#desc').val('');
       $('#url').val('');
       google.script.run
       .withSuccessHandler(function(obj){
         $('#title').val(obj.title);//set text to new values 
         $('#desc').val(obj.desc);
         $('#url').val(obj.url);
       })
       .saveData(obj);
      }
    </script>
  </head>
  <body>
    <? if (data.title !== "undefined") { ?>
        <input value="<?= data.title ?>" type="text" id="title" />
      <? } else { ?>
        <input value="" type="text">
    <? } ?>

    <? if (data.url !== "undefined") { ?>
        <input value="<?= data.url ?>" type="text" id="url" />
      <? } else { ?>
        <input value="" type="text">
    <? } ?>

    <? if (data.desc !== "undefined") { ?>
      <input value="<?= data.desc ?>" type="text" id="desc"/>
    <? } else { ?>
      <input value="" type="text">
    <? } ?>
    <input type="button" value="Save" onClick="saveData();" />
  </body>
</html>

gs:

function sendData() {
  var data = PropertiesService.getScriptProperties().getProperties();
  return data;
}

function runDialog() {
  var userInterface=HtmlService.createTemplateFromFile('aq7').evaluate();
  SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Testing');
}
//Save all of the values
function saveData(obj) {
  for(key in obj) {
    PropertiesService.getScriptProperties().setProperty(key,obj[key]);
  }
  return PropertiesService.getScriptProperties().getProperties();
}

问题:

undefined 覆盖属性。例如触发oninput="desc();captureInputs();"时,inputs是一个只有desc键的对象。

inputs={desc:"metaDescription"}

它不包含 urltitle 密钥。因此,它将是未定义的,并将保存为未定义。例如,

scriptProperties.setProperty('title', inputs.title);
当调用 desc();captureInputs() 时,

会将 title 存储为 undefined

解决方案:

改用 setProperties() 以避免覆盖存储的 属性 值:

function storeData(inputs) {
 var scriptProperties = PropertiesService.getScriptProperties();
/* scriptProperties.setProperty('title', inputs.title);
 scriptProperties.setProperty('url', inputs.url);
 scriptProperties.setProperty('desc', inputs.desc);*/
 scriptProperties.setProperties(inputs, false);
 }

参考文献: