如何将模态对话框返回的字符串处理成对象

问题描述 投票:0回答:1

我需要处理长度超过 50,000 个字符的 JSON 文件。为此,我想使用模式对话框文本区域。

我有一个带有两个文本区域的对话框。第一个是要粘贴的原始 json 文件,第二个是要输出处理后的文件的位置。

这一切都在测试模式下工作,我传递一个要处理的字符串

function processText (input){
  let processedText = input.toUpperCase(); 
  //Browser.msgBox(processedText)
  return processedText; 
}

我在模式的第一个文本区域中添加

ddddd
,然后单击模式中的处理按钮,我将
DDDDD
输出到第二个文本区域

但是如果我使用下面正确的

processText(input)
来处理我的 JSON,我无法将第一个文本区域的传递值作为对象,因此我可以使用
processText(input)

处理它

如何将返回到 processText(input) 的第一个文本区域的值处理为对象,以便我可以处理它

包含所有内容的 Google 表格 https://docs.google.com/spreadsheets/d/1VwMhr_imlmGrjcybVdfuJP7KFWBmgjWcIhL0n5bYLxY/edit?usp=sharing

谢谢

我的 gs 文件是

function showInputDialog() {
  var html = HtmlService.createTemplateFromFile('inputDialog').evaluate()
      .setTitle('Enter Long String')
      .setHeight(800)
      .setWidth(900);
  SpreadsheetApp.getUi().showModalDialog(html, 'Input');
}

function processText(input) {
  Browser.msgBox(typeof input)
  let inputJSON = JSON.parse(input);
  Browser.msgBox(typeof inputJSON)
  
   //Filter
  let processedText = inputJSON.filter(item => item.id && item.label && item.options)
                  .map(({ id, label, options }) => 
                         isEmptyObject(options) === false ? ({ id, label, options}):({ id, label}));

 processedText.forEach(i => Object.values(i.options ?? {}).forEach(i => delete i.count));                    

 Browser.msgBox(processedText)
 
 return JSON.stringify(processedText);
}

我的 HTML 文件是

<button id="copyTOP" style="margin-bottom: 25px;" onclick="copy()">Copy Redurced sumApp JSON</button><input style="width:31%;height:30px;" type="button" value="Close" onClick="google.script.host.close();" />
<!DOCTYPE html>
<html>
  <head>
    <base target="_top" />
        <link
            rel="stylesheet"
            //href="https://ssl.gstatic.com/docs/script/css/add-ons1.css"
      href="https://res.cloudinary.com/greater-then-the-sum/raw/upload/v1699896536/add-ons1a_ruvbar.css"
        />
        <style>
            .container {
                margin: 5px 5px 5px 5px;
            }
        </style>
  </head>
  <body>
    <H4>Raw sumApp JSON Input</H4>
    <textarea id="copyOrig" class="form-control"></textarea>
    <br>
    <H4>Output</H4>
    <textarea id="copyReduced" class="form-control"></textarea>
    <br><br><br>
    <button id="propersize" onclick="processAndDisplay()">Process</button>

  </body>
</html>
<br>
<br>
<button id="copyBottom" style="margin-bottom: 25px;" onclick="copy()">Copy Reduced sumApp JSON</button><input style="width:31%;height:30px;" type="button" value="Close" onClick="google.script.host.close();" />

<script>
  function processAndDisplay() {
    var inputText = document.getElementById('copyOrig').value;
    google.script.run.withSuccessHandler(successHandler).processText(JSON.stringify(inputText)); 
  }

  function successHandler(processedText) {
    document.getElementById('copyReduced').value = processedText; 
  } 
</script>


<script type="text/javascript">
 function copy() {
  let textarea = document.getElementById("copyReduced");
  textarea.select();
  document.execCommand("copy");
 }

 function copyBOTTOM() {
  let textarea = document.getElementById("copyNEW");
  textarea.select();
  document.execCommand("copy");
 } 
 </script>

JSON 文件

[
    {
    "id": "c3c6f410f58e5836431b473ebcf134756232d04f2bf35edff8",
    "component": "checkbox",
    "customFields": [

    ],
    "index": 0,
    "label": "Sector2",
    "options": {
      "62f92fab79ac81d933765bd0bbc4a1f5ea26cb3a088bcb4e6e": {
        "index": 0,
        "value": "Bob",
        "label": "Bob",
        "count": 1
      },
      "2fe91aa3567c0d04c521dcd2fc7e40d7622bb8c3f594d503da": {
        "index": 1,
        "value": "Student",
        "label": "Student",
        "count": 1
      },
      "c59ea1159f33b91a7f6edc6925be5e373fc543e4": {
        "index": 2,
        "value": "BBB",
        "label": "BBB",
        "count": 1
      },
      "c59ea1159f33b91a7f6edc6925be5e373fc54AAA": {
        "index": 3,
        "value": "Orange Duck",
        "label": "Orange Duck",
        "count": 1
      }
    },
    "required": false,
    "validation": "/.*/",
    "imported": false
  },
  {
    "id": "f794c6a52e793ee6f5c42cd5df6b4435236e3495e951709485",
    "component": "textInput",
    "customFields": [

    ],
    "index": 1,
    "label": "Brown Cow",
    "options": {
    },
    "required": false,
    "validation": "/.*/",
    "imported": false
  },
  {
    "id": "f794c6a52e793ee6f5c42cd5df6b4435236e3495e95170ZZZ",
    "component": "textInput",
    "customFields": [

    ],
    "index": 1,
    "label": "Red Fish",
    "options": {
    },
    "required": false,
    "validation": "/.*/",
    "imported": false
  },
 ];
google-apps-script modal-dialog
1个回答
0
投票

声明为

FileForTesting
的对象的结尾如下:

    "imported": false
  },
 ];

如果您将该源代码复制为文本并将其粘贴到对话框中,则传递到

processText()
的文本字符串将包含最后的逗号。

来自文档

然而,JSON 不允许所有尾随逗号。

删除逗号即可使其生效。

© www.soinside.com 2019 - 2024. All rights reserved.