用于在一组单元格中查找公式的一部分并将其替换为用户输入的脚本

A script to find a section of a formula in a group of cells and replace it with input from the user

我整理了一个出勤 google sheet,它将每周出勤选项卡中的数据提取到主 sheet 中,其中包含所有周。到目前为止,master sheet 已设置,以便可以通过复制 master sheet 的第一周并将其附加到 sheet 的最后一列来按下按钮来添加更多周。

所以我需要帮助的是在重复的新周的单元格中找到公式的一部分,在这种情况下是旧周选项卡的日期 '9/21-9/25'(完整的公式是 ='9/14-9/18'!$E) 并将其替换为用户输入的新周并提示,例如 '9/28-10/2'.

这是我到目前为止想出的代码。

function newWeek() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lastCol = sheet.getLastColumn();
  var rowCol = sheet.getLastRow();
  var maxCols = sheet.getMaxColumns();

  // Number of columns to duplicate//
  var numCols = 15
  //Selects the previous week//
  var range = sheet.getRange(1, lastCol - 14, rowCol, 15);
  //Inserts columns for next week//
  sheet.insertColumnsAfter(lastCol, numCols);
  //Copies previous weeks properties into new columns//
  range.copyTo(sheet.getRange(1, lastCol + 1, rowCol, 15));
}

如果您对我正在尝试做的事情有更简单的解决方案,请告诉我。只需按下按钮并输入新日期,所有内容都会创建并从新的周选项卡中提取。

ClassTextFinder can be used to search and replace within formulas using matchFormulaText()方法:

片段:

range.createTextFinder('9/21-9/25')
  .matchFormulaText(true)
  .replaceAllWith('9/28-10/2')