Paste COUNTIF公式的结果,而不是公式。

huangapple go评论60阅读模式
英文:

Paste COUNTIF Formula Results, Not The Formula

问题

我已经为此努力了数小时。

我有一个脚本,每隔24小时从另一个工作表中获取值,并将其存储在一个单元格中。这样我们就可以保持一个持续更新的顺序...

像这样:

10/4/23 - 10

11/4/23 - 15

12/4/23 - 24

等等

工作得很好... 但是,我遇到的问题是,我的脚本是通过粘贴公式而不是公式的结果来工作的。所以实际上我得到的是一个不断更新的工作表,像这样...

10/4/23 - 10

11/4/23 - 10

12/4/23 - 10

有没有人知道一种方法,可以粘贴公式的输出而不是公式本身?或者甚至有一种方法可以将结果复制并粘贴为仅值?

我的脚本在这里:

function addDate() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getActiveSheet();
  var tz = ss.getSpreadsheetTimeZone();
  var d = Utilities.formatDate(new Date(), tz, 'yyyy-MM-dd');
  sh.appendRow([d,'=COUNTIF(currentAffiliates!C:C,"Joined")','=COUNTIF(currentAffiliates!C:C,"Rejected")']);
}
英文:

Ive been wrestling with this for hours.

I have a script which every 24 hours takes the value from another sheet, and stores it into a cell. So we can keep a running order...

like

10/4/23 - 10

11/4/23 - 15

12/4/23 - 24

etc etc

Works fine....however, the problem I have is that my script works by pasting the formula, not the results of the formula. So what I actually end up with is a sheet that constantly updates itself, like....

10/4/23 - 10

11/4/23 - 10

12/4/23 - 10

Does anyone know a method whereby instead of pasting the formula, I can paste the output of the formula? Or even a method to copy the result, and paste it as value only?

My script is here;

function addDate() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getActiveSheet();
  var tz = ss.getSpreadsheetTimeZone();
  var d = Utilities.formatDate(new Date(), tz, 'yyyy-MM-dd');
  sh.appendRow([d,'=COUNTIF(currentAffiliates!C:C,"Joined")','=COUNTIF(currentAffiliates!C:C,"Rejected")']);
}

答案1

得分: 1

function addDate() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getActiveSheet();
  var tz = ss.getSpreadsheetTimeZone();
  var d = Utilities.formatDate(new Date(), tz, 'yyyy-MM-dd');
  
  // I modiifed the below script.
  var [joined, rejected] = ['joined', 'rejected'].map(e => range.createTextFinder(e).matchCase(false).matchEntireCell(true).findAll().length);
  sh.appendRow([d, joined, rejected]);
}
英文:

I believe your goal is as follows.

  • You want to put the actual values instead of the formulas of '=COUNTIF(currentAffiliates!C:C,"Joined")','=COUNTIF(currentAffiliates!C:C,"Rejected")'.

In this case, how about the following modification?

Modified script:

function addDate() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getActiveSheet();
  var tz = ss.getSpreadsheetTimeZone();
  var d = Utilities.formatDate(new Date(), tz, 'yyyy-MM-dd');
  
  // I modiifed the below script.
  var { joined, rejected } = ss.getRange("'currentAffiliates'!C:C").getDisplayValues()
    .reduce((o, [c]) => {
      ["joined", "rejected"].forEach(e => {
        if (e == c.toLowerCase()) {
          o[e]++
        }
      });
      return o;
    }, { joined: 0, rejected: 0 });
  sh.appendRow([d, joined, rejected]);
}
  • When this script is run, the actual values are appended instead of the formulas of '=COUNTIF(currentAffiliates!C:C,"Joined")','=COUNTIF(currentAffiliates!C:C,"Rejected")' to the sheet.

  • In this case, I thought that the following modification might be able to be also used.

    • From

        var { joined, rejected } = ss.getRange("'currentAffiliates'!C:C").getDisplayValues()
          .reduce((o, [c]) => {
            ["joined", "rejected"].forEach(e => {
              if (e == c.toLowerCase()) {
                o[e]++
              }
            });
            return o;
          }, { joined: 0, rejected: 0 });
      
    • To

        var [joined, rejected] = ["joined", "rejected"].map(e => range.createTextFinder(e).matchCase(false).matchEntireCell(true).findAll().length);
      

References:

huangapple
  • 本文由 发表于 2023年4月19日 16:18:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定