我無法找出如何獲取其中包含資料的特定列的最后一行(例如:
每次我想通過對話框在 ColA 中添加一行時,腳本都會獲取所有列的最后一行,而不僅僅是 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”(參見上面的代碼示例)寫入該列的下一個空白行。像這樣:

uj5u.com熱心網友回復:
如何獲取特定列的最后一行
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;
}
并在沒有 appendRow() 的情況下附加資料
GS:
function postMyInput(obj) {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
let col = obj.col;
let val = obj.entry;
let 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;
}
}
let lr= rcA.length - s;
sh.getRange(lr 1,col).setValue(val);
}
function launchPostItDialog() {
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutputFromFile('ah2'),'Input Dialog')
}
HTML:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<form>
<select name="col">
<option value="1">Col1</option>
<option value="2">Col2</option>
</select>
<input type="text" size="35" name='entry' placeholder="Enter Post Value" />
<input type="button" value="Post" onClick="postValue(this.parentNode)" />
</form>
<script>
function postValue(form) {
google.script.run.postMyInput(form);
}
</script>
</body>
</html>
演示:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/380440.html
