使用Google Apps脚本在公式中搜索和替换

问题描述 投票:0回答:1

我有一个脚本,每月制作 2 个主电子表格的副本。工作表名称始终包含月份和 2 位数年份。例如,本月的工作表称为 Oct23worksheet 和 Oct23schedule。在 Oct23schedule 表中,有几个引用 Oct23worksheet 的公式。我每个月都使用谷歌表格查找和替换功能手动更新公式,但我想自动执行此任务并将其添加到我的脚本中。具体来说,我只想搜索Oct23schedule表中的公式,找到每个公式中的master一词并将其替换为Oct23。

我认为这是可能的,但我什至不知道要搜索什么才能了解这样做。任何帮助将不胜感激。

google-apps-script replace formula
1个回答
0
投票

试试这个:

function createNewMonthlySheet() {

  // Update with IDs
  const masterWorksheetId = `*****`
  const masterScheduleId = `*****`
  const destinationFolder = DriveApp.getFolderById(`*****`)

  const monthYear =(() => {
    const date = new Date()
    const month = [
      `Jan`, `Feb`, `Mar`, `Apr`,
      `May`, `Jun`, `Jul`, `Aug`,
      `Sep`, `Oct`, `Nov`, `Dec`
    ][date.getMonth()]
    const year = String(date.getFullYear()).slice(-2)

    return `${month}${year}`
  })()

  DriveApp
    .getFileById(masterWorksheetId)
    .makeCopy(`${monthYear}worksheet`, destinationFolder)

  const newSchedule = DriveApp
    .getFileById(masterScheduleId)
    .makeCopy(`${monthYear}schedule`, destinationFolder)

  SpreadsheetApp
    .openById(newSchedule.getId())
    .getSheets()
    .forEach((sheet) => {

      const values = sheet
        .getDataRange()
        .getValues()

      const formulas = sheet
        .getDataRange()
        .getFormulas()

      const newValues = formulas
        .map((row, rowIndex) => {

          return [...row]
            .map((cell, cellIndex) => (cell.length)
              ? cell.replace(/master/g, monthYear)
              : values[rowIndex][cellIndex]
            )

        })

      sheet
        .getDataRange()
        .setValues(newValues)

      SpreadsheetApp.flush()

    })

}

评论:

function createNewMonthlySheet() {

  // Update with IDs.
  const masterWorksheetId = `*****`
  const masterScheduleId = `*****`
  const destinationFolder = DriveApp.getFolderById(`*****`)

  // Generates `MonthYear` string.
  const monthYear = (() => {
    const date = new Date()
    const month = [
      `Jan`, `Feb`, `Mar`, `Apr`,
      `May`, `Jun`, `Jul`, `Aug`,
      `Sep`, `Oct`, `Nov`, `Dec`
    ][date.getMonth()]
    const year = String(date.getFullYear()).slice(-2)

    return `${month}${year}`
  })()

  // Copy/rename masterWorksheet.
  DriveApp
    .getFileById(masterWorksheetId)
    .makeCopy(`${monthYear}worksheet`, destinationFolder)

  // Copy/rename masterSchedule.
  const newSchedule = DriveApp
    .getFileById(masterScheduleId)
    .makeCopy(`${monthYear}schedule`, destinationFolder)

  // Open schedule and replace all instances of `master` in formulas.
  SpreadsheetApp
    .openById(newSchedule.getId())
    .getSheets()
    // For each sheet...
    .forEach((sheet) => {

      // Get all values.
      const values = sheet
        .getDataRange()
        .getValues()

      // Get all formulas.
      const formulas = sheet
        .getDataRange()
        .getFormulas()

      const newValues = formulas
        // For each row...
        .map((row, rowIndex) => {
          return [...row]
            // For each cell...
            .map((cell, cellIndex) => (cell.length)
              // If a formula exists in the cell, replace any `master` with `MonthYear`.
              ? cell.replace(/master/g, monthYear)
              // If no formula, insert whatever value appears at this (row, col) index.
              : values[rowIndex][cellIndex]
            )
        })

      // Update the current sheet with the new values/formulas.
      sheet
        .getDataRange()
        .setValues(newValues)

      // Just to be safe.
      SpreadsheetApp.flush()

    })
    
}
© www.soinside.com 2019 - 2024. All rights reserved.