簡體   English   中英

用於根據日期調整字體樣式的腳本也會覆蓋單元格中的公式

[英]Script to adjust font-style based on date also overwrites formulas in cells

我正在使用電子表格進行整理,為了保持整潔,我使用了一個腳本,該腳本會根據日期更改行字體樣式,因此,如果日期已過,則該字體將變為斜體,變灰並具有直通。 該腳本效果很好,但是使用時會覆蓋電子表格中的所有公式。 有沒有一種方法可以編輯公式,使其不影響某些單元格? (對於尚未發生日期的行,它不應執行任何操作)。

這是我正在使用的腳本:

function formatOnDate() {
  var sh = SpreadsheetApp.getActive().getActiveSheet();
  var range = sh.getDataRange();
  var data = range.getValues();
  var color = '#AAA';// value you want
  var style = 'italic';// value you want
  var line = 'line-through';// value you want
  var fontColors = range.getFontColors();// get all font colors
  var fontLines = range.getFontLines();// lines
  var fontStyles = range.getFontStyles();//style
  var today = new Date();// include today in sheet

  //var today = new Date(new Date().setDate(new Date().getDate()-1));// exclude today... uncomment the one you use
  for(var n=1 ; n<data.length ; n++){ // start on row 2 so that headers are not changed
    if(data[n][0] < today){
      for(var c in data[0]){
        fontColors[n][c]=color;//set format
        fontLines[n][c]=line;//set format
        fontStyles[n][c]=style;//set format

      }
    }
  }
  //sh.getRange(1,1,data.length,data[0].length).clear();
  // now update sheet with new data and style
  sh.getRange(1,1,data.length,data[0].length).setValues(data).setFontColors(fontColors).setFontLines(fontLines).setFontStyles(fontStyles);
}

是否可以在不覆蓋單元格中的公式的情況下調整字體樣式? 是否有可能不影響尚未發生的日期的行? 如果不是,是否可能不影響某些列? (NQ列是具有公式的列)。

在您的腳本中, data不會被修改。 並且fontColorsfontLinesfontStyles的數組長度數也未修改。 那么如何修改呢?

修改后的腳本不會覆蓋數據。 僅FontColors,FontLines和FontStyles反映到range 因此,每個單元格的公式都不會受到影響。

來自:

sh.getRange(1,1,data.length,data[0].length).setValues(data).setFontColors(fontColors).setFontLines(fontLines).setFontStyles(fontStyles);

至 :

range.setFontColors(fontColors).setFontLines(fontLines).setFontStyles(fontStyles);

如果我誤解了您的問題,對不起。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM