需要在IFTTT的Insert_Row上设置Set_Formula

时间:2017-04-07 17:40:18

标签: google-apps-script google-sheets

新手Google Apps Scripter,

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

我似乎正确设置了公式,但是当添加新行时,公式显然不会自动填充到该新行中。当插入一行时,列A和B中的相应单元格不是空白,我想在该行中设置某些公式。

我到目前为止的脚本(见下文)确实给了我想要的公式,但仅限于Row1。 我喜欢脚本将相同的公式设置为插入的任何新行的相应单元格。

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

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

  var cell = sheet.getRange("C1");
  cell.setFormula('=IFERROR(MID($B2,SEARCH("details",$B2)+7,SEARCH(",",$B2)-SEARCH("details",$B2)-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
  var cell = sheet.getRange("D1");
  cell.setFormula('=IFERROR(TRIM(LEFT(SUBSTITUTE(MID(B2,FIND("$",B2),LEN(B2))," ",REPT(" ",100)),100)),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
  var cell = sheet.getRange("E1");
  cell.setFormula('=IFERROR(MID($B2,SEARCH("exceed",$B2)+7,SEARCH("%",$B2)-SEARCH("exceed",$B2)-6),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
  var cell = sheet.getRange("F1");
  cell.setFormula('=IFERROR(MID($B2,SEARCH("due",$B2)+3,SEARCH(";",$B2)-SEARCH("due",$B2)-3),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
  var cell = sheet.getRange("G1");
  cell.setFormula('=IFERROR(MID($B2,SEARCH("held on",$B2)+7,SEARCH(". Lottery",$B2)-SEARCH("held on",$B2)-7),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');
  var cell = sheet.getRange("H1");
  cell.setFormula('=IFERROR(MID($B2,SEARCH("posted by",$B2)+9,SEARCH(". ",$B2)-SEARCH("",$B2)-167),HYPERLINK("https://housing.sfgov.org/listings","See Housing Portal"))');

}

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

试试这个:

&#13;
&#13;
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.

  }
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这是我过去所做的事情。我很惊讶地发现有一个setFormula命令,因为我没有在这种情况下使用它,以下技术工作正常。

for(var i = 0;i < rowA[0].length; i++)
  {
    var initial_value = rowA[0][i];
    //rowA[0][i] = '=ArrayFormula(IF(Row($B:$B)=1,"' + initial_value + '",IF(LEN($B:$B),IF(REGEXMATCH($C:$C,"(?i)(' + initial_value + ')"),$D:$D,""),)))';
    rowA[0][i] = '=ArrayFormula(IF(Row(' + sr  + ')=1,"' + initial_value + '",IF(LEN(' + sr  + '),IF(REGEXMATCH(' + sr  + ',"(?i)(' + initial_value + ')"),' + vr  + ',""),)))';
  }
  rowrng.setValues(rowA);

与你的操作有点不同,我在运行setValues命令之前设置了所有数组值。但这已经有好几个月了。