Google 带有下拉列表和将行插入命名范围的工作表对话框
Google Sheets Dialog with Dropdown and Insert Row into Named Range
我有一个 sheet 我的公司用来跟踪公司内部的空缺班次。我希望脚本将行添加到正确 sheet 上的正确命名范围中。用户将从下拉列表中 select 一个帐户并输入多个未结班次。未结班次的数量将通过命名范围作为行添加到帐户中。 “This Week”的代码将通过命名范围将行插入到两个单独的 sheet 中。 (本周,这个 Week_writeonce)
这是我目前的代码,
function onOpen() {
SpreadsheetApp
.getUi()
.createMenu("Add Holes")
.addItem("Add Holes for This Week", "showThisWeekSidebar")
.addItem("Add Holes for Next Week", "showNextWeekSidebar")
.addToUi();
}
function showThisWeekSidebar() {
var html = HtmlService
.createTemplateFromFile('This Week');
// Add the dropdown lists to the template
html.namedRangesDPDWN = SpreadsheetApp.getActiveSheet().getRange("Named Ranges!NamedRanges").getValues();
// Keep adding the variables you need based on the ranges containing your dropdown values
// ...
// Prepares the template to be shown in the UI
html = html.evaluate()
.setTitle('Company Name')
.setWidth(200);
SpreadsheetApp.getUi().showSidebar(html);
}
function showNextWeekSidebar() {
var html = HtmlService
.createTemplateFromFile('Next Week');
// Add the dropdown lists to the template
html.namedRangesDPDWN = SpreadsheetApp.getActiveSheet().getRange("Named Ranges!NamedRanges").getValues();
// Keep adding the variables you need based on the ranges containing your dropdown values
// ...
// Prepares the template to be shown in the UI
html = html.evaluate()
.setTitle('Company Name')
.setWidth(200);
SpreadsheetApp.getUi().showSidebar(html);
}
下面是 HTML 代码。
<center>
<body bgcolor="#99a3a4">
<font face="arial" color="black">
<b>Add Holes for This Week</b><br><br>
<!-- Create input fields to accept values from the user -->
Account:<br>
<select id="Named Ranges">
<? for (let i in namedRangesDPDWN) { ?>
<option value="<?=namedRangesDPDWN[i]?>"><?=namedRangesDPDWN[i]?></option>
<? } ?>
</select>
<br><br>
<div class="block form-group">
<label for="numberOfHoles"># of Open Shifts</label><br>
<input type='text' name='email' id="email" required="required"/>
</div><br>
<div>
<button type="submit"id="submit-form">Submit</button>
</div>
正在尝试获取代码以计算命名范围内的行数,如果大于 2,则不考虑现有行数。
// Take the Account and number of holes from
// the sidebar and insert rows to the proper named range
function insertRowNext(account,n_rows) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ws = ss.getSheetByName('Next Week'); // Change to your sheet name
//Replace space with underscore
account=account.replace(/ /g,"_");
var nameRange = 'Next Week!'+account;
var range = ss.getRangeByName(nameRange);
var rangeRows = range.getNumRows();
n_rows-=1;
if(rangeRows == 2){
//add row
ws.insertRowsBefore(range.getLastRow(),n_rows);
//Show all rows in the namedRange
ws.showRows(range.getRow(),range.getNumRows()+n_rows);
}
else if (rangeRows > 2) {
//add row
ws.insertRowsBefore(range.getLastRow(),n_rows+1);
//Show all rows in the namedRange
ws.showRows(range.getRow(),range.getNumRows()+n_rows+1);
}
}
这是示例 sheet。 https://docs.google.com/spreadsheets/d/1zxdqkksdORfAUlmhnQmefKpxn8n-hB-3YeHdRiNLCLs/edit?usp=sharing
这是一个示例代码:(仅适用于本周添加孔菜单)
这个Week.html:
<center>
<body bgcolor="#99a3a4">
<font face="arial" color="black">
<b>Add Holes for This Week</b><br><br>
<!-- Create input fields to accept values from the user -->
Account:<br>
<select id="Named Ranges">
<? for (let i in namedRangesDPDWN) { ?>
<option value="<?=namedRangesDPDWN[i]?>"><?=namedRangesDPDWN[i]?></option>
<? } ?>
</select>
<br><br>
<div class="block form-group">
<label for="numberOfHoles"># of Open Shifts</label><br>
<input type='text' name='email' id="email" required="required"/>
</div><br>
<div>
<button type="submit"id="submit-form" onclick="processInput()">Submit</button>
</div>
<script>
function processInput(){
var account = document.getElementById("Named Ranges").value;
var count = document.getElementById("email").value;
google.script.run.withSuccessHandler(success).insertRowThis(account,count);
}
function success(){
alert("Row added successfully");
}
</script>
修改:
- 我添加了一个脚本来在单击提交按钮时调用
processInput()
。
- 在
processInput()
中,它将获取所选帐户和开放班次数。然后将它们作为参数传递并在服务器端调用 insertRowThis()
。
We used google.script.run.withSuccessHandler(function) to call server-side Apps Script function which will then call the client-side callback function to run when the server responds. In this example the client-side callback is success()
addHolesMenu.gs
function insertRowThis(account,n_rows) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ws = ss.getSheetByName('This Week');
//Replace space with underscore
account=account.replace(/ /g,"_");
var nameRange = 'This Week!'+account;
n_rows-=1;
Logger.log(nameRange);
Logger.log(n_rows);
if(n_rows>0){
//add row
var range = ss.getRangeByName(nameRange);
Logger.log(range.getRow());
Logger.log(range.getLastRow());
ws.insertRowsBefore(range.getLastRow(),n_rows);
//Show all rows in the namedRange
ws.showRows(range.getRow(),range.getNumRows()+n_rows);
}
}
修改:
- 我添加了接受 2 个参数的
insertRowThis()
- 因为此菜单用于在 sheet
This Week
中添加行。我使用 Spreadsheet.getSheetByName(name). 选择了特定的 sheet
- 将帐户名称中的
space
替换为 underscore
- 从行输入计数中减去 1
- 创建 nameRange 字符串以获取 sheet
This Week
中的特定范围。然后使用 Spreadsheet.getRangeByName(name)
- 在 namedRange 末尾之前插入所需的行数。这背后的原因是,如果我们在 namedRange 的最后一行之后插入该行,它不会包含在 namedRange 本身中。要在添加其他行时自动调整 namedRange,应将其添加到 namedRange 的最后一行之前。
- 显示 namedRange 下的所有行,包括新添加的行
输出:
(更新)
在E列添加公式:
// ==========================================================================================================
// Take the Account and number of holes from
// the sidebar and insert rows to the proper named range
function insertRowThis(account,n_rows) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ws = ss.getSheetByName('This Week'); // Change to your sheet name
//Replace space with underscore
account=account.replace(/ /g,"_");
var nameRange = 'This Week!'+account;
var range = ss.getRangeByName(nameRange);
var rangeRows = range.getNumRows();
n_rows-=1;
Logger.log(range.getA1Notation());
Logger.log(rangeRows);
Logger.log(range.getRow());
Logger.log(range.getLastRow());
if(rangeRows == 2){
//add row
ws.insertRowsBefore(range.getLastRow(),n_rows);
//Show all rows in the namedRange
ws.showRows(range.getRow(),range.getNumRows()+n_rows);
}
else if (rangeRows > 2) {
//revert subtracted row count
n_rows+=1;
//add row
ws.insertRowsBefore(range.getLastRow(),n_rows);
//Show all rows in the namedRange
ws.showRows(range.getRow(),range.getNumRows()+n_rows);
}
//Get formula in the header row in column E
var formula = ws.getRange(range.getRow(),5).getFormula();
Logger.log(formula);
//Add formula in the newly added rows
for (var i = 0; i<n_rows; i++){
Logger.log("row: "+(range.getLastRow()+i));
ws.getRange(range.getLastRow()+i,5).setFormula(formula);
}
}
您可以参考此示例代码调整您的其他菜单。不需要的日志可以去掉,我只是调试用的
我有一个 sheet 我的公司用来跟踪公司内部的空缺班次。我希望脚本将行添加到正确 sheet 上的正确命名范围中。用户将从下拉列表中 select 一个帐户并输入多个未结班次。未结班次的数量将通过命名范围作为行添加到帐户中。 “This Week”的代码将通过命名范围将行插入到两个单独的 sheet 中。 (本周,这个 Week_writeonce)
这是我目前的代码,
function onOpen() {
SpreadsheetApp
.getUi()
.createMenu("Add Holes")
.addItem("Add Holes for This Week", "showThisWeekSidebar")
.addItem("Add Holes for Next Week", "showNextWeekSidebar")
.addToUi();
}
function showThisWeekSidebar() {
var html = HtmlService
.createTemplateFromFile('This Week');
// Add the dropdown lists to the template
html.namedRangesDPDWN = SpreadsheetApp.getActiveSheet().getRange("Named Ranges!NamedRanges").getValues();
// Keep adding the variables you need based on the ranges containing your dropdown values
// ...
// Prepares the template to be shown in the UI
html = html.evaluate()
.setTitle('Company Name')
.setWidth(200);
SpreadsheetApp.getUi().showSidebar(html);
}
function showNextWeekSidebar() {
var html = HtmlService
.createTemplateFromFile('Next Week');
// Add the dropdown lists to the template
html.namedRangesDPDWN = SpreadsheetApp.getActiveSheet().getRange("Named Ranges!NamedRanges").getValues();
// Keep adding the variables you need based on the ranges containing your dropdown values
// ...
// Prepares the template to be shown in the UI
html = html.evaluate()
.setTitle('Company Name')
.setWidth(200);
SpreadsheetApp.getUi().showSidebar(html);
}
下面是 HTML 代码。
<center>
<body bgcolor="#99a3a4">
<font face="arial" color="black">
<b>Add Holes for This Week</b><br><br>
<!-- Create input fields to accept values from the user -->
Account:<br>
<select id="Named Ranges">
<? for (let i in namedRangesDPDWN) { ?>
<option value="<?=namedRangesDPDWN[i]?>"><?=namedRangesDPDWN[i]?></option>
<? } ?>
</select>
<br><br>
<div class="block form-group">
<label for="numberOfHoles"># of Open Shifts</label><br>
<input type='text' name='email' id="email" required="required"/>
</div><br>
<div>
<button type="submit"id="submit-form">Submit</button>
</div>
正在尝试获取代码以计算命名范围内的行数,如果大于 2,则不考虑现有行数。
// Take the Account and number of holes from
// the sidebar and insert rows to the proper named range
function insertRowNext(account,n_rows) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ws = ss.getSheetByName('Next Week'); // Change to your sheet name
//Replace space with underscore
account=account.replace(/ /g,"_");
var nameRange = 'Next Week!'+account;
var range = ss.getRangeByName(nameRange);
var rangeRows = range.getNumRows();
n_rows-=1;
if(rangeRows == 2){
//add row
ws.insertRowsBefore(range.getLastRow(),n_rows);
//Show all rows in the namedRange
ws.showRows(range.getRow(),range.getNumRows()+n_rows);
}
else if (rangeRows > 2) {
//add row
ws.insertRowsBefore(range.getLastRow(),n_rows+1);
//Show all rows in the namedRange
ws.showRows(range.getRow(),range.getNumRows()+n_rows+1);
}
}
这是示例 sheet。 https://docs.google.com/spreadsheets/d/1zxdqkksdORfAUlmhnQmefKpxn8n-hB-3YeHdRiNLCLs/edit?usp=sharing
这是一个示例代码:(仅适用于本周添加孔菜单)
这个Week.html:
<center>
<body bgcolor="#99a3a4">
<font face="arial" color="black">
<b>Add Holes for This Week</b><br><br>
<!-- Create input fields to accept values from the user -->
Account:<br>
<select id="Named Ranges">
<? for (let i in namedRangesDPDWN) { ?>
<option value="<?=namedRangesDPDWN[i]?>"><?=namedRangesDPDWN[i]?></option>
<? } ?>
</select>
<br><br>
<div class="block form-group">
<label for="numberOfHoles"># of Open Shifts</label><br>
<input type='text' name='email' id="email" required="required"/>
</div><br>
<div>
<button type="submit"id="submit-form" onclick="processInput()">Submit</button>
</div>
<script>
function processInput(){
var account = document.getElementById("Named Ranges").value;
var count = document.getElementById("email").value;
google.script.run.withSuccessHandler(success).insertRowThis(account,count);
}
function success(){
alert("Row added successfully");
}
</script>
修改:
- 我添加了一个脚本来在单击提交按钮时调用
processInput()
。 - 在
processInput()
中,它将获取所选帐户和开放班次数。然后将它们作为参数传递并在服务器端调用insertRowThis()
。
We used google.script.run.withSuccessHandler(function) to call server-side Apps Script function which will then call the client-side callback function to run when the server responds. In this example the client-side callback is
success()
addHolesMenu.gs
function insertRowThis(account,n_rows) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ws = ss.getSheetByName('This Week');
//Replace space with underscore
account=account.replace(/ /g,"_");
var nameRange = 'This Week!'+account;
n_rows-=1;
Logger.log(nameRange);
Logger.log(n_rows);
if(n_rows>0){
//add row
var range = ss.getRangeByName(nameRange);
Logger.log(range.getRow());
Logger.log(range.getLastRow());
ws.insertRowsBefore(range.getLastRow(),n_rows);
//Show all rows in the namedRange
ws.showRows(range.getRow(),range.getNumRows()+n_rows);
}
}
修改:
- 我添加了接受 2 个参数的
insertRowThis()
- 因为此菜单用于在 sheet
This Week
中添加行。我使用 Spreadsheet.getSheetByName(name). 选择了特定的 sheet
- 将帐户名称中的
space
替换为underscore
- 从行输入计数中减去 1
- 创建 nameRange 字符串以获取 sheet
This Week
中的特定范围。然后使用 Spreadsheet.getRangeByName(name) - 在 namedRange 末尾之前插入所需的行数。这背后的原因是,如果我们在 namedRange 的最后一行之后插入该行,它不会包含在 namedRange 本身中。要在添加其他行时自动调整 namedRange,应将其添加到 namedRange 的最后一行之前。
- 显示 namedRange 下的所有行,包括新添加的行
输出:
(更新)
在E列添加公式:
// ==========================================================================================================
// Take the Account and number of holes from
// the sidebar and insert rows to the proper named range
function insertRowThis(account,n_rows) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ws = ss.getSheetByName('This Week'); // Change to your sheet name
//Replace space with underscore
account=account.replace(/ /g,"_");
var nameRange = 'This Week!'+account;
var range = ss.getRangeByName(nameRange);
var rangeRows = range.getNumRows();
n_rows-=1;
Logger.log(range.getA1Notation());
Logger.log(rangeRows);
Logger.log(range.getRow());
Logger.log(range.getLastRow());
if(rangeRows == 2){
//add row
ws.insertRowsBefore(range.getLastRow(),n_rows);
//Show all rows in the namedRange
ws.showRows(range.getRow(),range.getNumRows()+n_rows);
}
else if (rangeRows > 2) {
//revert subtracted row count
n_rows+=1;
//add row
ws.insertRowsBefore(range.getLastRow(),n_rows);
//Show all rows in the namedRange
ws.showRows(range.getRow(),range.getNumRows()+n_rows);
}
//Get formula in the header row in column E
var formula = ws.getRange(range.getRow(),5).getFormula();
Logger.log(formula);
//Add formula in the newly added rows
for (var i = 0; i<n_rows; i++){
Logger.log("row: "+(range.getLastRow()+i));
ws.getRange(range.getLastRow()+i,5).setFormula(formula);
}
}
您可以参考此示例代码调整您的其他菜单。不需要的日志可以去掉,我只是调试用的