google-apps-script - Increment only non-empty cells in a range with a single button on Google Sheets
问题描述
I want to loop through a range of cells of a column and increment only the non-empty cells by 1, with a click of a single button.
I tried a code that was later found to be for VBA Excel and not for Google Sheets; which I'm currently looking for.
The blank cells should remain unaffected. Thank you.
解决方案
使用单个按钮增加非空单元格
function incrementNonEmptyCellsInARange(rA1) {
var rA1=rA1 || 'A1:C3';
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet1');
var change=false;
try {
var rg=sh.getRange(rA1);
}
catch(e) {
SpreadsheetApp.getUi().alert(e + '\r\n Current Range: ' + rA1);
return;
}
if(rg) {
var vA=rg.getValues();
for(var i=0;i<vA.length;i++) {
for(var j=0;j<vA[i].length;j++) {
if(typeof vA[i][j] == 'number') {
vA[i][j]+=1;
change=true;
}
}
}
if(change) {
rg.setValues(vA);
}
}
}
这是按钮
和一个文本框,用于以 A1 表示法输入所需范围。
function showMySideBar() {
var html='Range: <input type="text" id="txt1" /><br /><input type="button" value="Increment" onClick="doIt();" />';
html+='<script>function doIt(){var txt=document.getElementById("txt1").value;google.script.run.incrementNonEmptyCellsInARange(txt);}console.log("My Code");</script>';
var userInterface=HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showSidebar(userInterface);
}
推荐阅读
- node.js - react-scripts 使用旧版本的 node 和带有 SSL 的 npm 开始崩溃
- r - 取消列出数据框列表并在循环中分配名称 - R
- amazon-web-services - 如何在 aws serverless 上安排按需任务
- sql - 如何修复单行子查询在选择查询中返回多行问题以便以所需的方式获得结果?
- excel - 从 VBA 中的非组织数据中创建一个计数不同的数据透视表
- c++ - 如何使用 using 或 typedef 创建一个将自身作为参数的函数类型?
- python - 从子模块导入父包
- java - 通过网络连接计算机
- python - Python 通过 IP 地址在域上查找计算机名称
- javascript - 如何更改网格中框的颜色