OnEdit触发器的正确​​语法 [英] Correct Syntax for OnEdit Trigger

查看:198
本文介绍了OnEdit触发器的正确​​语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在每次编辑电子表格时都得到一个其他人写的调用函数的应用程序。我一直在寻找谷歌开发者网站 https://developers.google.com/apps-script / understanding_triggers ,并尝试了许多不同的语法选项,但目前为止它们都没有工作。下面是它的功能(我没有包含任何我尝试过的语法选项,只是名为'onEdit'的函数本身)。我希望此功能执行的是计算指定范围内具有非白色背景的单元格数量,并在编辑电子表格时更新使用此附加脚本的单元格中显示的计数。顺便说一句,有没有人知道更改单元格的背景颜色是否会计算为编辑电子表格?
$ b

I'm trying to get an app that somebody else wrote to call the function each time the spreadsheet is edited. I've been looking at the google developers website https://developers.google.com/apps-script/understanding_triggers and have tried a number of different options for syntax, but none of them have worked so far. Here's the function as it stands (I haven't included any of the syntax options I tried, just the function itself named 'onEdit'). All I want this function to do is count the number of cells with a non-white background in the specified range and update the count displayed in the cell that uses this add-on script when the spreadsheet is edited. Incidentally, does anyone know whether changing the background color of a cell counts as editing the spreadsheet? Thank you.

function onEdit(range) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();



  var r = sheet.getRange(range);
  var i = 0;
  var colors  = r.getBackgroundColors();

  for(c in colors) {

    if(colors[c] != "white") {
      i++;
    }
  }

  return i;


}


推荐答案

onEdit 是一个事件处理程序。你将无法从中返回任何东西。只需在外部初始化 i ,即全局范围。

onEdit is an event handler. you wont be able to return something from it. just initialise i outside, i.e., to the global scope.

var i = 0;    
function onEdit(range) {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getActiveSheet();



      var r = sheet.getRange(range);

      var colors  = r.getBackgroundColors();

      for(c in colors) {

        if(colors[c] != "white") {
          i++;
        }
      }


    }

这篇关于OnEdit触发器的正确​​语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆