【问题标题】:Google Sheets Script to find formulas and copy/paste them into notes谷歌表格脚本查找公式并将它们复制/粘贴到笔记中
【发布时间】:2022-04-12 00:23:05
【问题描述】:

我喜欢复制/粘贴所有旧工作表作为值以节省资源,但我不想删除公式,因为我依赖它们在将来重新创建它们(有些非常复杂)。我认为一个不错的选择是将每个公式复制到其单元格的注释中,然后再粘贴为值,所以我认为我可以编写一个脚本来执行此操作,最好是在整个工作表中。我承认我去这里时正在谷歌搜索,但我在正确的轨道上吗?我是否有正确的变量来开始处理循环?

        function CopyFormulaToNote() {

        /* I dont' want to get just the active sheet but all of them
        look into how to do that */

      var ss=SpreadsheetApp.getActive().getActiveSheet();
      var lr=ss.getLastRow();
      var lc=ss.getLastColumn();
      var range=ss.getRange(1,1,lr,lc);

        /* Find the first formula in the entire spreadsheet
        I doubt it will just find all formulas per se without specifying that a formula begins with =
        although there is a getFormula function...
        and if I do have to specify that I think what I have below says to look for = as the first character */

      var search=range.createTextFinder().matchFormulaText("^[=]");  //search for formula ie first character =
      var result=search.getCurrentMatch();      //I *think* this would get the 1st result
      var cell=result.getCell(row, column);    //get the cell range of the result
      var note=cell.setNote(note);             //create a cell note for that cell



      /* and now create a loop to do it everywhere that includes */

result.CopyTo(note);  //copy the result into the note

    }

【问题讨论】:

  • @Tanaike 辉煌!我想可能有更简单的方法。非常感谢您抽出宝贵的时间。
  • 感谢您的回复。很高兴您的问题得到解决。

标签: google-apps-script google-sheets


【解决方案1】:
  • 您希望从 Google 电子表格的所有工作表中的所有单元格中检索公式。
  • 您希望将检索到的公式作为注释放入每个单元格中。
  • 您希望使用 Google Apps 脚本实现此目的。

如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。

我认为在您的情况下,getFormulas()setNotes() 的脚本可以更简单。

示例脚本:

function myFunction() {
  SpreadsheetApp
  .getActiveSpreadsheet()
  .getSheets()
  .forEach(function(sheet) {
    var range = sheet.getDataRange();
    range.setNotes(range.getFormulas());
  });
}

参考资料:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多