我無法將自定義對話框中的資料放入作業表中的特定范圍,如下所示:

每次我在對話框中輸入新資料時,它都應該在該特定范圍內附加一個新行。到目前為止,我正在使用 appendRow ,但問題是 appendRow 在作業表中的所有列中計數,因此我在特定范圍內有空白單元格,如下所示:

這是我正在使用的代碼之一:
//STUNDE EINTRAGEN VIA START
function showDialogKBSTART() {
gotoSheetSchueler();
var widget = HtmlService.createHtmlOutputFromFile("formKBSTART.html").setHeight(450).setWidth(450);
SpreadsheetApp.getUi().showModalDialog(widget, "Stunde eintragen");
}
function appendRowFromFormSubmitKBSTART(formKBSTART) {
SpreadsheetApp.getActiveSheet()
.appendRow([formKBSTART.Datum, formKBSTART.Inhalt, formKBSTART.Sozialform, formKBSTART.Dauer, formKBSTART.Lehrkraft,, formKBSTART.Ausfall]);
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('KlassenbuchGesamt')
.appendRow([SpreadsheetApp.getActiveSheet().getName(), formKBSTART.Datum, formKBSTART.Inhalt, formKBSTART.Sozialform, formKBSTART.Dauer, formKBSTART.Lehrkraft, formKBSTART.Ausfall]);
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("A9:A").setNumberFormat("dd.MM.yyyy");
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("A9:F").sort({column: 1, ascending: false});
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('KlassenbuchGesamt').getRange("B2:B").setNumberFormat("dd.MM.yyyy");
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('KlassenbuchGesamt').getRange("A2:G").sort({column: 2, ascending: false});
Utilities.sleep(500);
SpreadsheetApp.getActiveSpreadsheet().toast("Stunde eingetragen!","??",3);
Utilities.sleep(500);
hide();
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function submitForm() {
google.script.run.appendRowFromFormSubmitKBSTART(document.getElementById("KBstart"));
document.getElementById("form").style.display = "none";
}
</script>
</head>
<body>
<div>
<div id="form">
<form id="KBstart">
<label for="Datum"><b>Datum</b></label></br>
<input type="date" id="Datum" name="Datum"><br><br>
<label for="Inhalt"><b>Inhalt</b></label></br>
<input type="text" id="Inhalt" name="Inhalt" size="60"><br><br>
<label for="Sozialform"><b>Einzel oder Gruppe?</b></label><br>
<input type="checkbox" id="Ez" name="Sozialform" value="Ez">
<label for="Ez">Einzel</label><br>
<input type="checkbox" id="Gr" name="Sozialform" value="Gr">
<label for="Gr">Gruppe</label><br><br>
<label for="Dauer"><b>Dauer</b></label></br>
<select id="Dauer" name="Dauer">
<option value="45">45</option>
<option value="60">60</option>
<option value="90">90</option>
</select>
<div>
</br>
<label for="Lehrkraft"><b>Lehrkraft</b></label></br>
<select id="Lehrkraft" name="Lehrkraft">
<option value="Alexander Michaelis">Alexander Michaelis</option>
<option value="Thomas Weider">Thomas Weider</option>
<option value="Merve Besser">Merve Besser</option>
</select>
<div>
</br>
<label for="Ausfall"><b>Ausfall?</b></label><br>
<input type="checkbox" id="yes" name="Ausfall" value="E">
<label for="yes">Entschuldigt</label><br>
<input type="checkbox" id="no" name="Ausfall" value="UE">
<label for="no">Unentschuldigt</label><br><br>
<input type="button" value="Eintragen" onclick="submitForm();google.script.host.close()">
<button type="reset" value="Reset">Neu</button>
</form>
</body>
</html>
有人可以幫助我獲取特定范圍內的資料并在不使用 appendRow 的情況下追加新行嗎?謝謝!
更新!!:建議使用以下代碼:
function appendRowFromFormSubmitKBSTART(formKBSTART) {
var kb = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('KlassenbuchGesamt');
var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var col = 0; // column A
// get the number of last empty row in column 'A'
var col_data = sh.getDataRange().getValues().map(x=>x[0]);
var last_row = col_data.length - col_data.reverse().findIndex(x=>x!='') 1;
// get the range
var myRange = sh.getRange('A' last_row ':F' last_row);
// data
var myData = [
formKBSTART.Datum,
formKBSTART.Inhalt,
formKBSTART.Sozialform,
formKBSTART.Dauer,
formKBSTART.Lehrkraft,
formKBSTART.Ausfall
];
// set the data into the range
myRange.setValues(myData);
ss.getRange("A9:A").setNumberFormat("dd.MM.yyyy");
ss.getRange("A9:F").sort({column: 1, ascending: false});
kb.appendRow([SpreadsheetApp.getActiveSheet().getName(), formKBSTART.Datum, formKBSTART.Inhalt, formKBSTART.Sozialform, formKBSTART.Dauer, formKBSTART.Lehrkraft, formKBSTART.Ausfall]);
kb.getRange("B2:B").setNumberFormat("dd.MM.yyyy");
kb.getRange("A2:G").sort({column: 2, ascending: false});
ss.toast("Stunde eingetragen!","??",3);
hide();
}
UPDATE2:這是最簡單形式的代碼:
function showDialogKBSTART() {
var widget = HtmlService.createHtmlOutputFromFile("formKBSTART.html").setHeight(450).setWidth(450);
SpreadsheetApp.getUi().showModalDialog(widget, "Stunde eintragen");
}
function appendRowFromFormSubmitKBSTART(formKBSTART) {
var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var col = 0; // column A
// get the number of last empty row in column 'A'
var col_data = sh.getDataRange().getValues().map(x=>x[0]);
var last_row = col_data.length - col_data.reverse().findIndex(x=>x!='') 1;
// get the range
var myRange = sh.getRange('A' last_row ':F' last_row);
// data
var myData = [
formKBSTART.Datum,
formKBSTART.Inhalt,
formKBSTART.Sozialform,
formKBSTART.Dauer,
formKBSTART.Lehrkraft,
formKBSTART.Ausfall
];
// set the data into the range
myRange.setValues(myData);
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function submitForm() {
google.script.run.appendRowFromFormSubmitKBSTART(document.getElementById("KBstart"));
document.getElementById("form").style.display = "none";
}
</script>
</head>
<body>
<div>
<div id="form">
<form id="KBstart">
<label for="Datum"><b>Datum</b></label></br>
<input type="date" id="Datum" name="Datum"><br><br>
<label for="Inhalt"><b>Inhalt</b></label></br>
<input type="text" id="Inhalt" name="Inhalt" size="60"><br><br>
<label for="Sozialform"><b>Einzel oder Gruppe?</b></label><br>
<input type="checkbox" id="Ez" name="Sozialform" value="Ez">
<label for="Ez">Einzel</label><br>
<input type="checkbox" id="Gr" name="Sozialform" value="Gr">
<label for="Gr">Gruppe</label><br><br>
<label for="Dauer"><b>Dauer</b></label></br>
<select id="Dauer" name="Dauer">
<option value="45">45</option>
<option value="60">60</option>
<option value="90">90</option>
</select>
<div>
</br>
<label for="Lehrkraft"><b>Lehrkraft</b></label></br>
<select id="Lehrkraft" name="Lehrkraft">
<option value="Alexander Michaelis">Alexander Michaelis</option>
<option value="Thomas Weider">Thomas Weider</option>
<option value="Merve Besser">Merve Besser</option>
</select>
<div>
</br>
<label for="Ausfall"><b>Ausfall?</b></label><br>
<input type="checkbox" id="yes" name="Ausfall" value="E">
<label for="yes">Entschuldigt</label><br>
<input type="checkbox" id="no" name="Ausfall" value="UE">
<label for="no">Unentschuldigt</label><br><br>
<input type="button" value="Eintragen" onclick="submitForm();google.script.host.close()">
<button type="reset" value="Reset">Neu</button>
</form>
</body>
</html>
所以基本上是這樣的:

uj5u.com熱心網友回復:
顯然你需要range.setValues()方法。像這樣的東西:
var myRange = SpreadsheetApp.getActiveSheet().getRange('A9:F9');
var myData = [
formKBSTART.Datum,
formKBSTART.Inhalt,
formKBSTART.Sozialform,
formKBSTART.Dauer,
formKBSTART.Lehrkraft,
formKBSTART.Ausfall
];
myRange.setValues([myData]); // <--- square brackets!
當然你每次都需要計算目的地范圍。首先它可以是A9:F9然后它會是A10:F10,等等。
您可以通過以下方式找到列末尾的第一個空單元格:
var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var col = 0; // column A
var col_data = sh.getDataRange().getValues().map(x=>x[col]);
var last_row = col_data.length - col_data.reverse().findIndex(x=>x!='') 1;
因此,代碼的一部分可能如下所示:
var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// get the number of first empty row at the end of column 'A'
var col_data = sh.getDataRange().getValues().map(x=>x[0]);
var last_row = col_data.length - col_data.reverse().findIndex(x=>x!='') 1;
// get the range
var myRange = sh.getRange('A' last_row ':F' last_row);
// data
var myData = [
formKBSTART.Datum,
formKBSTART.Inhalt,
formKBSTART.Sozialform,
formKBSTART.Dauer,
formKBSTART.Lehrkraft,
formKBSTART.Ausfall
];
// set the data into the range
myRange.setValues([myData]); // <---- square brackets!
另一種方法:您可以每次洗掉特定范圍內的空單元格(并將資料向上移動)。不過,我認為這是一種效率較低的方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/380439.html
