簡體   English   中英

如何使用“appendRow”獲取特定列的最后一行和 append 數據到該行

[英]How to get the last row of a specific column and append data to that row with "appendRow"

我無法找出如何獲取其中包含數據的特定列的最后一行(例如: Determining the last row in a single column )並在我的代碼中與“appendRow”一起實現它,所以我可以 append 數據到該特定列中的下一個空白行。

這是我所擁有的示例表: 在此處輸入圖像描述

每次我想通過對話框在 ColA 中 append 一行時,腳本都會獲取所有列的最后一行,而不僅僅是 ColA。

這是我正在使用的代碼示例:

//dialog 1
function showDialog1() {
  
  var widget = HtmlService.createHtmlOutputFromFile("dialog1.html").setHeight(250).setWidth(700);
  SpreadsheetApp.getUi().showModalDialog(widget, "Dialog 1");
}

function appendRowFromFormSubmitDIALOG1(formdialog1) {

    SpreadsheetApp.getActiveSheet().appendRow([formdialog1.datadialog1]); 
  
    
}




//dialog 2
function showDialog2() {
  
  var widget = HtmlService.createHtmlOutputFromFile("dialog2.html").setHeight(250).setWidth(700);
  SpreadsheetApp.getUi().showModalDialog(widget, "Dialog 2");
}

function appendRowFromFormSubmitDIALOG2(formdialog2) {

    SpreadsheetApp.getActiveSheet().appendRow([,formdialog2.datadialog2]); 
  
    
}
<!DOCTYPE html>
<html>
<head>
  <base target="_top">
  <script>
    function submitForm() {
      google.script.run.appendRowFromFormSubmitDIALOG1(document.getElementById("dialog1"));
      document.getElementById("form").style.display = "none";
      
    }
  </script>
</head>
<body>
  <div>
  <div id="form">
  
  <form id="dialog1">

  
    <label for="datadialog1"><b>datadialog1</b></label></br>
    <input type="text" id="datadialog1" name="datadialog1" size="60"><br><br>
    

      <input type="button" value="submit" onclick="submitForm();google.script.host.close()">
       
  </form>

</body>
</html>

所以我的問題是如何獲取特定列的最后一行並讓“appendRow”將數據從“appendRowFromFormSubmit”(參見上面的代碼示例)寫入該列的下一個空白行。 像這樣: 在此處輸入圖像描述

如何獲取特定列的最后一行

function lastRowOfCol(col, sh, ss) {
  var ss = ss || SpreadsheetApp.getActive();
  var sh = sh || ss.getActiveSheet();
  var col = col || sh.getActiveCell().getColumn();
  var rcA = [];
  if (sh.getLastRow()) { rcA = sh.getRange(1, col, sh.getLastRow(), 1).getValues().flat().reverse(); }
  let s = 0;
  for (let i = 0; i < rcA.length; i++) {
    if (rcA[i].toString().length == 0) {
      s++;
    } else {
      break;
    }
  }
  return rcA.length - s;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM