获取(新创建的)Textarea 的值并将其插入 Google Sheets

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

我有许多文本区域,并且可以创建新的文本区域。我使用 insertValue 按钮将文本区域的值插入到电子表格中。我的代码如下: HTML:

<!DOCTYPE html>
<html>
<body>
  <div class="container">
    <input type="text" id="qnumberDS" placeholder="Enter question">

    <textarea id="contentDS" placeholder="Enter question content"></textarea>

    <div class="options">
      <div class="option">
        <input type="checkbox" id="ch1" class="checkbox">
        <textarea id="a" class="option-textarea" placeholder="Enter option A"></textarea>
      </div>
      <div class="option">
        <input type="checkbox" id="ch2" class="checkbox">
        <textarea id="b" class="option-textarea" placeholder="Enter option "></textarea>
      </div>
      <div class="option">
        <input type="checkbox" id="ch3" class="checkbox">
        <textarea id="c" class="option-textarea" placeholder="Enter option C"></textarea>
      </div>
      <div class="option">
        <input type="checkbox" id="ch4" class="checkbox">
        <textarea id="d" class="option-textarea" placeholder="Enter option D"></textarea>
      </div>
      </div>

    <button id="insertRow">Add options</button>

    <button id="insertValue">Send data</button>
  </div>

  <script>
    
const qnumberDSInput = document.getElementById('qnumberDS');
const contentDSInput = document.getElementById('contentDS');
const optionsContainer = document.querySelector('.options');
const insertRowButton = document.getElementById('insertRow');
const insertValueButton = document.getElementById('insertValue');


function insertRow() {
  const newOption = document.createElement('div');
  newOption.classList.add('option');

  const newCheckbox = document.createElement('input');
  newCheckbox.type = 'checkbox';
  newCheckbox.id = `ch${optionsContainer.children.length + 1}`;
  newCheckbox.classList.add('checkbox');
  newOption.appendChild(newCheckbox);

  const newTextarea = document.createElement('textarea');
  newTextarea.id = String.fromCharCode(97 + optionsContainer.children.length);
  newTextarea.classList.add('option-textarea');
  newTextarea.placeholder = `Nhập phương án ${String.fromCharCode(97 + optionsContainer.children.length)}`;
  newOption.appendChild(newTextarea);

  optionsContainer.appendChild(newOption);
}


function insertValue() {
  const title = qnumberDSInput.value;
  const content = contentDSInput.value;

  const data = [];

  optionsContainer.querySelectorAll('.option').forEach((option, index) => {
    const checkbox = option.querySelector('.checkbox');
    const textarea = option.querySelector('.option-textarea');

    const optionValue = textarea.value;
    const isChecked = checkbox.checked;

    const optionData = {
      value: optionValue,
      isCorrect: isChecked ? 'Đ' : 'S'
    };
    if (!data[data.length - 1].options) {
      data[data.length - 1].options = [];
    }

    data[data.length - 1].options.push(optionData);
  });

  google.script.run.enterNameDS(data);
}

insertRowButton.addEventListener('click', insertRow);
insertValueButton.addEventListener('click', insertValue);

  </script>
</body>
</html>

js:

function enterNameDS(data) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var shTitle = ss.getSheetByName('title');
  var sh = ss.getSheetByName('tests');

  var numPart1 = Number(shTitle.getRange('E8').getValue());

 data.forEach(question => {
    const formulas = [question.title, `="${question.content}"`];

    question.options.forEach(option => {
      formulas.push(`=if(column()=${option.position + 3};"${option.value}";"")`);
    });

    sh.getRange(numPart1 + 1, 1, 1, formulas.length).setFormulas([formulas]);

    numPart1++;
  });

 shTitle.getRange('E8').setValue(numPart1);
}

不知道错误是在HTML中还是在js中。我不知道如何查找 HTML 中嵌入的脚本代码中的错误。

我真的需要你的帮助来查找这段代码中的错误。谢谢你。

javascript html google-apps-script button textarea
1个回答
0
投票

问题在于 insertValue() 中,您需要将选项推入数据数组,而不是推入 data[].options。

即:

data.push(optionData);

如果您不知道如何调试 html 文件中的 javascript,那么我建议您将其移至它自己的 .js 文件中,您知道如何调试它。只需记住在 html 区域中包含一个脚本命令即可执行。

 <script defer src="/javascript_libraries/myJavascript.js"></script>

这是工作的 insertValue() 函数:

function insertValue() {
  const title = qnumberDSInput.value;
  const content = contentDSInput.value;

  const data = [];

  optionsContainer.querySelectorAll('.option').forEach((option, index) => {
    const checkbox = option.querySelector('.checkbox');
    const textarea = option.querySelector('.option-textarea');

    const optionValue = textarea.value;
    const isChecked = checkbox.checked;

    const optionData = {
      value: optionValue,
      isCorrect: isChecked ? 'Đ' : 'S'
    };

    // NOTE: This is push replaces the other two
    data.push(optionData);
  });

  google.script.run.enterNameDS(data);
}
© www.soinside.com 2019 - 2024. All rights reserved.