Apps脚本:想要自动插入一个复选框到新行的第一列单元格



设置:Google Sheets with the sidebar form

我的目标:当我点击"按钮,我想:

  1. 自动生成新行
  2. 用表单输入自动填充新行
  3. 在新行的第一个单元格中自动插入一个复选框

我的挑战:

我很难让第三步工作:"在新行的第一个单元格中自动插入一个复选框"

在附件图片所示的示例中,在点击"按钮在我的侧边栏表单…我的输入在新行中正确填充。复选框确实呈现,但是,它呈现在我上次点击鼠标的活动单元格上。它不会自动插入到新行的第一列"a25"。

表格截图这是我添加新行的代码:

function addNewRow(rowData) {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ws = ss.getSheetByName("Home Maintenance");

ws.appendRow([rowData.status,rowData.task,rowData.location,rowData.frequency,rowData.month]);

}

这是我的复选框功能代码:

function insertCheckbox(){
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sh=ss.getActiveSheet();
var activeRange=sh.getActiveRange();
var checkbox=SpreadsheetApp.newDataValidation().requireCheckbox().setAllowInvalid(false).build();
activeRange.setDataValidation(checkbox).setValue(false);
}

下面是我的html文件中的脚本:

<script>
function afterButtonClicked() {
var task = document.getElementById("task");
var location = document.getElementById("location");
var frequency = document.getElementById("frequency");
var month = document.getElementById("month");
var rowData = {
status: status.value,
task: task.value,
location: location.value,
frequency: frequency.value,
month: month.value
};
google.script.run.insertCheckbox();
google.script.run.addNewRow(rowData);
}
document.getElementById("mainButton").addEventListener("click", afterButtonClicked);
</script>

我对编程很陌生。我认为问题的一部分是我在insertCheckbox()函数中使用getActiveRange()方法。我的直觉一直指向addNewRow()函数中的appendRow(),并且我想将CHECKBOX设置为"状态"的值。我不知道该往哪个方向走。提前感谢您的帮助。

function insertCheckbox(){
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sh=ss.getActiveSheet();
sh.getActiveRange().insertCheckboxes();
}

ref

最新更新