【问题标题】:Increment only non-empty cells in a range with a single button on Google Sheets使用 Google 表格上的一个按钮仅增加一个范围内的非空单元格
【发布时间】:2023-03-04 16:45:01
【问题描述】:

我想通过单击一个按钮来遍历列的一系列单元格并仅将非空单元格增加 1。

我尝试了一个代码,后来发现它适用于 VBA Excel 而不是 Google 表格;我目前正在寻找。

空白单元格应保持不受影响。谢谢。

【问题讨论】:

  • 这是 VBA/Excel。您是否有理由将问题标记为 google-apps-script 和 google-sheets?
  • but to no avail 是什么意思?它在做什么不应该或不应该做什么?如果出现什么错误?
  • 嗨,我添加了这些标签,因为问题与此处提到的问题非常相似:stackoverflow.com/q/32813855/10370106
  • 我收到错误“Missing ; before statement. (line 1, file "Code")"
  • 您已发布 VBA 代码。 VBA 在 Google 表格中不起作用。 VBA 和 Google Apps 脚本完全不同。

标签: google-apps-script google-sheets


【解决方案1】:

使用单个按钮增加非空单元格

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 Notation 中输入所需范围。

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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多