我可以使用Insert_Row作为cell.setFormula的触发器吗?

时间:2017-04-07 20:09:07

标签: google-apps-script google-sheets

新手Google Apps Scripter,

我有一个IFTTT applet,通过电子邮件在此电子表格中添加一行:Data Test

感谢令人惊叹的StackOverflow社区,我现在能够 手动 添加一行,用正确的参考设置所需的公式,我想要脚本到 自动 将这些相同的公式设置为插入的任何新行的相应单元格。

例如,我的IFTTT.com自动化将使用文本填充单元格A6和B6(即,在链接的电子表格中创建下一个空行) - 我需要当前输入的所有公式然后应用于B6(而不是B2) )

var ss = SpreadsheetApp.getActiveSpreadsheet();
function onOpen() {
  var items = [
    {name: 'Add Row', functionName: 'addrow'},
  ];
    ss.addMenu('Add Row', items);
    }

function addrow() {
  var ui = SpreadsheetApp.getUi(); 
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var result = ui.prompt(
      'Enter number of Row',
      ui.ButtonSet.OK_CANCEL);

    // Process the user's response.
  var button = result.getSelectedButton();
  var rownum = result.getResponseText();
  if (button == ui.Button.OK) {
    // User clicked "OK".
  sheet.insertRowAfter(rownum);
  var cell = sheet.getRange("C" + rownum);
  cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("details",$B' + rownum +')+7,SEARCH(",",$B' + rownum +')-SEARCH("details",$B' + rownum +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
  var cell = sheet.getRange("D" + rownum);
  cell.setFormula('=IFERROR(TRIM(LEFT(SUBSTITUTE(MID(B' + rownum +',FIND("$",B' + rownum +'),LEN(B' + rownum +'))," ",REPT(" ",100)),100)),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
  var cell = sheet.getRange("E" + rownum);
  cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("exceed",$B' + rownum +')+7,SEARCH("%",$B' + rownum +')-SEARCH("exceed",$B' + rownum +')-6),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
  var cell = sheet.getRange("F" + rownum);
  cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("due",$B' + rownum +')+3,SEARCH(";",$B' + rownum +')-SEARCH("due",$B' + rownum +')-3),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
  var cell = sheet.getRange("G" + rownum);
  cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("held on",$B' + rownum +')+7,SEARCH(". Lottery",$B' + rownum +')-SEARCH("held on",$B' + rownum +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
  var cell = sheet.getRange("H" + rownum);
  cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("posted by",$B' + rownum +')+9,SEARCH(". ",$B' + rownum +')-SEARCH("",$B' + rownum +')-167),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
  } else if (button == ui.Button.CANCEL) {
    // User clicked "Cancel".

  } else if (button == ui.Button.CLOSE) {
    // User clicked X in the title bar.

  }
}

要清楚,我想做的事情(而不是添加菜单来手动插入一行)有一个脚本,可以检测插入新行的时间,触发以下内容:

  var cell = sheet.getRange("C" + rownum);
  cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("details",$B' + rownum +')+7,SEARCH(",",$B' + rownum +')-SEARCH("details",$B' + rownum +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
  var cell = sheet.getRange("D" + rownum);
  cell.setFormula('=IFERROR(TRIM(LEFT(SUBSTITUTE(MID(B' + rownum +',FIND("$",B' + rownum +'),LEN(B' + rownum +'))," ",REPT(" ",100)),100)),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
  var cell = sheet.getRange("E" + rownum);
  cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("exceed",$B' + rownum +')+7,SEARCH("%",$B' + rownum +')-SEARCH("exceed",$B' + rownum +')-6),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
  var cell = sheet.getRange("F" + rownum);
  cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("due",$B' + rownum +')+3,SEARCH(";",$B' + rownum +')-SEARCH("due",$B' + rownum +')-3),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
  var cell = sheet.getRange("G" + rownum);
  cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("held on",$B' + rownum +')+7,SEARCH(". Lottery",$B' + rownum +')-SEARCH("held on",$B' + rownum +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
  var cell = sheet.getRange("H" + rownum);
  cell.setFormula('=IFERROR(MID($B' + rownum +',SEARCH("posted by",$B' + rownum +')+9,SEARCH(". ",$B' + rownum +')-SEARCH("",$B' + rownum +')-167),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:1)

这适用于编辑电子表格的任何时间。您可能必须将其测试为“ANYTIME”,在编辑时添加功能。似乎不是最好的方法,但它有点像你在寻找。

function onEdit(e){
    var ui = SpreadsheetApp.getUi(); 
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheets()[0];
    var range = e.range;
    var range2 = range.getRow();
    var destrange = range2 + 1;
  
    // inserts blank row after last edit.
    sheet.insertRowAfter(range2);
  
    // adds the formulas
    var cell = sheet.getRange("C" + destrange);
    cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("details",$B' + destrange +')+7,SEARCH(",",$B' + destrange +')-SEARCH("details",$B' + destrange +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
    var cell = sheet.getRange("D" + destrange);
    cell.setFormula('=IFERROR(TRIM(LEFT(SUBSTITUTE(MID(B' + destrange +',FIND("$",B' + destrange +'),LEN(B' + destrange +'))," ",REPT(" ",100)),100)),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
    var cell = sheet.getRange("E" + destrange);
    cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("exceed",$B' + destrange +')+7,SEARCH("%",$B' + destrange +')-SEARCH("exceed",$B' + destrange +')-6),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
    var cell = sheet.getRange("F" + destrange);
    cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("due",$B' + destrange +')+3,SEARCH(";",$B' + destrange +')-SEARCH("due",$B' + destrange +')-3),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
    var cell = sheet.getRange("G" + destrange);
    cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("held on",$B' + destrange +')+7,SEARCH(". Lottery",$B' + destrange +')-SEARCH("held on",$B' + destrange +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
    var cell = sheet.getRange("H" + destrange);
    cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH("posted by",$B' + destrange +')+9,SEARCH(". ",$B' + destrange +')-SEARCH("",$B' + destrange +')-167),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');

}

答案 1 :(得分:0)

根据此相关SO帖子Google Script: function that insert new row copying functions/formulas from last row中的建议,尝试使用上一行的getFormula()setFormula()到新行。

getFomula()代码:

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];

 // This assumes we have a function in B5 that sums up
 // B2:B4
 var range = sheet.getRange("B5");

 // Logs the calculated value and the formula
 Logger.log("Calculated value: %s Formula: %s",
            range.getValue(),
            range.getFormula());

setFormula()代码:

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];

 var cell = sheet.getRange("B5");
 cell.setFormula("=SUM(B3:B4)");

希望这有帮助。

答案 2 :(得分:0)

另一种方法 - 现在如果最后一行B列不是空白,它将填写公式。适用于每次编辑。

function onEdit(){
    var ui = SpreadsheetApp.getUi(); 
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheets()[0];
    var lastRow = ss.getLastRow();
    var lastRow2 = lastRow+1;
    var s = ss.getSheetByName('sheet1');
    var lRow = s.getRange(lastRow, 2).getValue();
    var newRow = s.getRange(lastRow2, 2).getValue();
     if(lRow != ""){

    // adds the formulas
    var cell = sheet.getRange("C" + lastRow2);
    cell.setFormula('=IFERROR(MID($B' + lastRow2 +',SEARCH("details",$B' + lastRow2 +')+7,SEARCH(",",$B' + lastRow2 +')-SEARCH("details",$B' + lastRow2 +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
    var cell = sheet.getRange("D" + lastRow2);
    cell.setFormula('=IFERROR(TRIM(LEFT(SUBSTITUTE(MID(B' + lastRow2 +',FIND("$",B' + lastRow2 +'),LEN(B' + lastRow2 +'))," ",REPT(" ",100)),100)),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
    var cell = sheet.getRange("E" + lastRow2);
    cell.setFormula('=IFERROR(MID($B' + lastRow2 +',SEARCH("exceed",$B' + lastRow2 +')+7,SEARCH("%",$B' + lastRow2 +')-SEARCH("exceed",$B' + lastRow2 +')-6),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
    var cell = sheet.getRange("F" + lastRow2);
    cell.setFormula('=IFERROR(MID($B' + lastRow2 +',SEARCH("due",$B' + lastRow2 +')+3,SEARCH(";",$B' + lastRow2 +')-SEARCH("due",$B' + lastRow2 +')-3),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
    var cell = sheet.getRange("G" + lastRow2);
    cell.setFormula('=IFERROR(MID($B' + lastRow2 +',SEARCH("held on",$B' + lastRow2 +')+7,SEARCH(". Lottery",$B' + lastRow2 +')-SEARCH("held on",$B' + lastRow2 +')-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
    var cell = sheet.getRange("H" + lastRow2);
    cell.setFormula('=IFERROR(MID($B' + lastRow2 +',SEARCH("posted by",$B' + lastRow2 +')+9,SEARCH(". ",$B' + lastRow2 +')-SEARCH("",$B' + lastRow2 +')-167),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
     }
}