简体   繁体   中英

I want change cell values from some sheet in many sheet by google spread sheet script

function UpdateCell()
{
  const NotationRange = "J1";
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheets = ss.getSheets();
  const year = 2020;
  sheets.forEach
  (
    (sheet) => 
    { const sheetName = sheet.getName();
       { sheet.getRange(NotationRange).setValue(year+'.'+sheetName);
       }
    }
  )
}

in google spreadsheet script

upper code is work well and fast but, It change all of sheet

sheet name has rule. so, it can be distinguished

what I want is change cell value in specific sheet that sheet name contain [week]

in my sheet name are [Reference],[01.01],[01.02],,,,[12.31],[week01],[week02],,,[week53]

so I tried but fail..

function UpdateCell()
{
  const NotationRange = "B2:D5"
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheets = ss.getSheets();
  const year = 2020
  const checkDailyWord = ".";
  const checkWeekWord = "week";
  var DC = new RegExp('\\b' + checkDailyWord + '\\b');
  var WC = new RegExp('\\b' + checkWeekWord + '\\b');
  sheets.forEach
  (
    (sheet) => 
    { const sheetName = sheet.getName();
       if( DC.sheetName == 1 )
       { sheet.getRange(NotationRange).setValue(year+'.'+sheetName);
       }
       if( WC.sheetName == 1 )
       { sheet.getRange(NotationRange).setValue(year+'week'+sheetName);
       }
    }
  )
}

Modification points:

  • new RegExp has no property of sheetName . Ref By this, undefined is alwasy returned. I think that this might be the reason of your issue.
  • In your case, I think that includes can be used for the if statement.

When above points are reflected to your script, it becomes as follows.

Modified script:

function UpdateCell() {
  const NotationRange = "B2:D5"
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheets = ss.getSheets();
  const year = 2020
  const checkDailyWord = ".";
  const checkWeekWord = "week";
  sheets.forEach(sheet => {
    const sheetName = sheet.getName();
    if (sheetName.includes(checkDailyWord) && !sheetName.includes(checkWeekWord)) {
      sheet.getRange(NotationRange).setValue(year+'.'+sheetName);
    } else if (!sheetName.includes(checkDailyWord) && sheetName.includes(checkWeekWord)) {
      sheet.getRange(NotationRange).setValue(year+'week'+sheetName);
    }
  });
}
  • When this modified script is run, in your sample sheet names, 01.01,01.02,12.31 and week01,week02,week53 run sheet.getRange(NotationRange).setValue(year+'.'+sheetName) and sheet.getRange(NotationRange).setValue(year+'week'+sheetName) , respectively.

Note:

  • In this modified script, from your sample sheet names, it supposes that the sheets with the sheet name includes both week and . are not used.

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM