为什么我无法在Google Apps Script中使用任何方法?

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

Why I cannot use any methods in Google Apps Script?

问题

当我在电子表格上运行此代码时,出现“TypeError: table_range.setBackgroundObject不是函数”错误。
我显然漏掉了某些东西,但我不知道是什么。任何帮助都将不胜感激!
英文:

I'm creating a function that checks if certain scores are equal to max score. If all of them meet this condition I want the entire range of cells to change it's background color.

function ColorsForTable(selected_scores,max_score,table_range) {
  var isMax=true;
  for(var  i=0;i<selected_scores.length;i++){
    if(selected_scores[i][0]!==max_score){
      isMax=false;
      break;
    }
  }
  if(isMax){
    table_range.setBackgroundObject('#D4AF37');
  }
}

When I run this code on my spreadsheet I get
TypeError: table_range.setBackgroundObject is not a function

I'm clearly missing something but I don't know what. Any help is highly appreciated!

答案1

得分: 0

根据你的回复,我理解你正在使用名为 ColorsForTable(selected_scores, max_score, table_range) 的自定义函数。在这种情况下,有几个问题。

修改要点:

  • 对于=ColorsForTable(K35:P35,20,K15:P34)情况,K15:P34是单元格的值。因此,table_range 不是 Class Range。这就是你当前遇到的TypeError: table_range.setBackgroundObject is not a function错误的原因。
  • setBackgroundObject 的参数应该是 SpreadsheetApp.Color 对象。但是你使用的是字符串 #D4AF37。应该使用 setBackground(''#D4AF37'')
  • 在当前阶段,不能在自定义函数中使用 setBackgroundObjectsetBackground。即使将 table_range 修改为 Class Range 对象并使用 setBackground(''#D4AF37''),还会出现类似 Exception: You do not have permission to call setBackground 的错误。
  • 对于 K35:P35 的值,它是 [[1, 2, 3,,,]]。在这种情况下,selected_scores[i][0][1, 2, 3,,,]。因此,只比较单元格 "K35"。

我认为这些要点可能是回答“为什么我不能在 Google Apps Script 中使用任何方法”的答案。如果你想使用这个脚本,作为样本修改,你可以尝试在脚本编辑器中运行你的脚本。当然,可以通过自定义菜单、电子表格上的按钮、侧边栏和对话框来执行此脚本。

修改后的脚本:

请将以下脚本复制并粘贴到电子表格的脚本编辑器中,并设置工作表名称并保存脚本。selected_scores, max_score, table_range 的值来自你的回复。要运行此脚本,请在脚本编辑器中运行 sample() 函数。

function ColorsForTable(selected_scores, max_score, table_range) {
  var isMax = true;
  for (var i = 0; i < selected_scores.length; i++) {
    if (selected_scores[i] !== max_score) {
      isMax = false;
      break;
    }
  }
  if (isMax) {
    table_range.setBackground('&#39;#D4AF37&#39;');
  }
}

// 请使用脚本编辑器运行此函数。
function sample() {
  var sheetName = "Sheet1"; // 请设置你的工作表名称。
  var selected_scores = "K35:P35";
  var max_score = 20;
  var table_range = "K15:P34";

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  ColorsForTable(
    sheet.getRange(selected_scores).getValues()[0],
    max_score,
    sheet.getRange(table_range)
  );
}
  • 在这个修改后的脚本中,当所有 "K35:P35" 的值都等于 20 时,将 "K15:P34" 的背景颜色更改为 "#D4AF37"。

注意:

  • 这只是对你当前问题的简单修改脚本。请根据你的实际情况进行修改。

  • 作为另一个样本脚本,如果使用侧边栏,可以使用以下样本脚本。在这种情况下,请使用脚本编辑器运行函数 main

    function ColorsForTable(selected_scores, max_score, table_range) {
      var isMax = true;
      for (var i = 0; i < selected_scores.length; i++) {
        if (selected_scores[i] !== max_score) {
          isMax = false;
          break;
        }
      }
      if (isMax) {
        table_range.setBackground('&#39;#D4AF37&#39;');
      }
    }
    
    function main(e) {
      if (!e) {
        var html = `<input id="selected_scores" value="K35:P35" placeholder="Please input selected_scores."> <input type="number" id="max_score" value="20" placeholder="Please input max_score."> <input id="table_range" value="K15:P34" placeholder="Please input table_range."> <input type="button" value="Run script" onclick="main()"><script>function main() {
          const values = ["selected_scores","max_score", "table_range"].map(e => document.getElementById(e).value);
          console.log(values)
          google.script.run.withSuccessHandler(_ => console.log("Done.")).main(values);
        }</script>`;
        SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutput(html));
        return;
      }
      var [selected_scores, max_score, table_range] = e;
      var sheet = SpreadsheetApp.getActiveSheet();
      ColorsForTable(
        sheet.getRange(selected_scores).getValues()[0],
        Number(max_score),
        sheet.getRange(table_range)
      );
    }
    
    • 当运行函数 main 时,会在电子表格中打开一个侧边栏。在此样本中,你的样本值已经设置。单击按钮时,脚本将运行。ColorsForTable 会使用有效值运行。
  • 例如,如果想在 isMax 为 false 时设置其他颜色,请将 ColorsForTable 修改如下。

    • if (isMax) {
        table_range.setBackground('&#39;#D4AF37&#39;');
      }
      
    • table_range.setBackground(isMax ? '&#39;#D4AF37&#39;' : '&#39;#ffffff&#39;'); // 请将 '&#39;#ffffff&#39;' 设置为你期望的颜色。
      

参考:

英文:

From your following reply,

> I run it like this: =ColorsForTable(K35:P35 , 20 , K15:P34) (added spaces for visibility) In K35:P35 there are only numbers and in K15:P34 there are some text variables.

I understood that you are using your function ColorsForTable(selected_scores,max_score,table_range) as a custom function. In this case, there are several issues.

Modification points:

  • In the case of =ColorsForTable(K35:P35,20,K15:P34), K15:P34 is the cell values. By this, table_range is not Class Range. This is the reason for your current issue of TypeError: table_range.setBackgroundObject is not a function.
  • And, the argument of setBackgroundObject is SpreadsheetApp.Color object. But, you use a string #D4AF37. It is required to be setBackground(&#39;#D4AF37&#39;).
  • And, in the current stage, setBackgroundObject and setBackground cannot be used with the custom function. Even when table_range is modified to Class Range object and setBackground(&#39;#D4AF37&#39;) is used, another error like Exception: You do not have permission to call setBackground occurs.
  • And, in the case of the value of K35:P35, it is [[1, 2, 3,,,]]. In this case, selected_scores[i][0] is [1, 2, 3,,,]. In this case, only the cell "K35" is compared.

I thought that these points might be an answer to Why I cannot use any methods in Google Apps Script?. If you want to use this script, as a sample modification, how about running your script with the script editor? Of course, this script can be executed by the custom menu, a button on the Spreadsheet, a sidebar, and a dialog.

Modified script:

Please copy and paste the following script to the script editor of Spreadsheet and set the sheet name and save the script. The values of selected_scores, max_score, table_range are from your reply. When you use this script, please run the function sample() with the script editor. By this, the script is run.

function ColorsForTable(selected_scores, max_score, table_range) {
  var isMax = true;
  for (var i = 0; i &lt; selected_scores.length; i++) {
    if (selected_scores[i] !== max_score) {
      isMax = false;
      break;
    }
  }
  if (isMax) {
    table_range.setBackground(&#39;#D4AF37&#39;);
  }
}

// Please run this function with the script editor.
function sample() {
  var sheetName = &quot;Sheet1&quot;; // Please set your sheet name.
  var selected_scores = &quot;K35:P35&quot;;
  var max_score = 20;
  var table_range = &quot;K15:P34&quot;;

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  ColorsForTable(
    sheet.getRange(selected_scores).getValues()[0],
    max_score,
    sheet.getRange(table_range)
  );
}
  • From If all of them meet this condition I want the entire range of cells to change it&#39;s background color., in this modified script, when this script is run, when all values of "K35:P35" are the same value with 20, the background color of "K15:P34" is changed to "#D4AF37".

Note:

  • This is a simple modified script for explaining the reason for your current issue. So, please modify this to your actual situation.

  • As another sample script, when a sidebar is used, the sample script is as follows. In this case, please run the function main with the script editor.

    function ColorsForTable(selected_scores, max_score, table_range) {
      var isMax = true;
      for (var i = 0; i &lt; selected_scores.length; i++) {
        if (selected_scores[i] !== max_score) {
          isMax = false;
          break;
        }
      }
      if (isMax) {
        table_range.setBackground(&#39;#D4AF37&#39;);
      }
    }
    
    function main(e) {
      if (!e) {
        var html = `&lt;input id=&quot;selected_scores&quot; value=&quot;K35:P35&quot; placeholder=&quot;Please input selected_scores.&quot;&gt; &lt;input type=&quot;number&quot; id=&quot;max_score&quot; value=&quot;20&quot; placeholder=&quot;Please input max_score.&quot;&gt; &lt;input id=&quot;table_range&quot; value=&quot;K15:P34&quot; placeholder=&quot;Please input table_range.&quot;&gt; &lt;input type=&quot;button&quot; value=&quot;Run script&quot; onclick=&quot;main()&quot;&gt;&lt;script&gt;function main() {
      const values = [&quot;selected_scores&quot;,&quot;max_score&quot;, &quot;table_range&quot;].map(e =&gt; document.getElementById(e).value);
      console.log(values)
      google.script.run.withSuccessHandler(_ =&gt; console.log(&quot;Done.&quot;)).main(values);
    }&lt;/script&gt;`;
        SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutput(html));
        return;
      }
      var [selected_scores, max_score, table_range] = e;
      var sheet = SpreadsheetApp.getActiveSheet();
      ColorsForTable(
        sheet.getRange(selected_scores).getValues()[0],
        Number(max_score),
        sheet.getRange(table_range)
      );
    }
    
    • When the function main is run, a sidebar is opened to the Spreadsheet. In this sample, the values of your sample have already been set. When you click a button, the script is run. And, ColorsForTable is run with valid values.
  • For example, if you want to set other color when isMax is false, please modify ColorsForTable as follows.

    • From

      if (isMax) {
        table_range.setBackground(&#39;#D4AF37&#39;);
      }
      
    • To

      table_range.setBackground(isMax ? &#39;#D4AF37&#39; : &#39;#ffffff&#39;); // Please set &#39;#ffffff&#39; to your expected color.
      

References:

huangapple
  • 本文由 发表于 2023年5月26日 08:35:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76336974.html
匿名

发表评论

匿名网友

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

确定