我只能在有人添加新行时才能运行脚本 [英] How can I run a script only when someone adds new rows

查看:125
本文介绍了我只能在有人添加新行时才能运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:

我有下面的脚本,它在OnEdit中运行,但我想调整此脚本只在有人向表中添加新行时运行。

脚本:



 函数DataValidation2(e)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
if(s.getName()==Sheet1){
var cell = s.getRange('C3:C');
var rule = SpreadsheetApp.newDataValidation()。setAllowInvalid(false).requireValueInList(['0:15:00','0:20:00'])。build();
cell.setDataValidation(rule);




$ b $ p
$ b

如果可能的话,只有当有人添加新行时才执行此脚本使用boton添加包括在表的最后?



最好的问候,

解决方案 div>

有一个特殊的触发器来检测这种变化: onChange



您甚至可以按照文档中的描述以编程方式创建它:

  var sheet = SpreadsheetApp.getActive(); 
ScriptApp.newTrigger(myFunction)
.forSpreadsheet(表单)
.onChange()
.create();

然后像这样的函数将在每次更改时调用,查看行数和如果它改变了,做什么事情,如果改变是别的什么都不做。



您可以添加更多条件来处理其他情况,例如做什么id行()删除

  function myFunction(){
var numRows = SpreadsheetApp.getActiveSpreadsheet()。getActiveSheet()。getMaxRows );
if(ScriptProperties.getProperty('numberOfRows')){
var nRows = Number(ScriptProperties.getProperty('numberOfRows'));
if(nRows //做些事情,因为行已经添加在这张表中
ScriptProperties.setProperty('numberOfRows',numRows); //用当前值更新值


ScriptProperties.setProperty('numberOfRows',numRows); //如果触发器被另一个原因调用,则创建一个当前值为简单更新的起始值
}






编辑Wchiquito的评论
$ b pre
$ b pre
$ b $ p

 function initializeTrigger(){//只需运行一次即可创建触发器
var sheet = SpreadsheetApp.getActive();
ScriptApp.newTrigger(myFunction)
.forSpreadsheet(表单)
.onChange()
.create();
}

函数myFunction(e){
Logger.log(e.changeType);
if(e.changeType =='INSERT_ROW'){
// do something
Browser.msgBox('New row(s)added');
}
}


Situation:

I have the following script, that runs in OnEdit, but I want to tune this script to run only when someone adds new rows to the sheet.

Script:

function DataValidation2(e)
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getActiveSheet();
  if (s.getName() == "Sheet1"){
  var cell = s.getRange('C3:C');
  var rule = SpreadsheetApp.newDataValidation().setAllowInvalid(false).requireValueInList(['0:15:00', '0:20:00']).build();
  cell.setDataValidation(rule);
  }
}

If possible execute this script only when someone add new row using the boton add include at the end of the sheet?

Best Regards,

解决方案

There is a special trigger to detect such changes : onChange

You can even create it programmatically as described in the documentation :

var sheet = SpreadsheetApp.getActive();
 ScriptApp.newTrigger("myFunction")
   .forSpreadsheet(sheet)
   .onChange()
   .create();

and then a function like this that will be called on every change that looks at the number of rows and do something if it changed...and does nothing if the change was something else.

You can add more conditions to handle other cases, for example what to do id rows where deleted

function myFunction(){
  var numRows = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getMaxRows();
  if(ScriptProperties.getProperty('numberOfRows')){
    var nRows = Number(ScriptProperties.getProperty('numberOfRows'));
    if(nRows<numRows){
      //do something because rows have been added in this sheet
      ScriptProperties.setProperty('numberOfRows',numRows);// update value with current value
    }
  }
  ScriptProperties.setProperty('numberOfRows',numRows);// create a start value with current value of simply update if the trigger was called for another reason
}


EDIT following Wchiquito 's comment

here is another (simpler) version that works pretty well too :

function initializeTrigger(){ // run this only once to create a trigger if necessary
  var sheet = SpreadsheetApp.getActive();
 ScriptApp.newTrigger("myFunction")
   .forSpreadsheet(sheet)
   .onChange()
   .create();
}

function myFunction(e){
  Logger.log(e.changeType);
  if(e.changeType=='INSERT_ROW'){
    // do Something
    Browser.msgBox('New row(s) added');
  }
}

这篇关于我只能在有人添加新行时才能运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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