将工作表范围定义为单独的工作表

时间:2017-04-13 20:00:10

标签: google-apps-script google-sheets

目前正在使用此Google Apps脚本在编辑的同一行填充公式,但我希望看到它在同一文件中的不同工作表上填充单元格。

似乎我需要将destrange定义为单独目标表上的下一个空行,但我不知道该怎么做。

function onEdit(e){
var ui = SpreadsheetApp.getUi(); 
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = e.range;
var sheet = range.getSheet()
var newSheet = ss.getSheetByName("Apps")
var sheetName = sheet.getName()
if(sheetName != "Form"){
   return                            //exit function
}  
var columnOfCellEdited = range.getColumn();
if (columnOfCellEdited === 1) 
 var range2 = range.getRow();
 var destrange = newSheet.getLastRow()
 sheet = newSheet
     // adds the formulas
     var cell = sheet.getRange("C" + destrange);
  cell.setFormula('=Form!$B' + destrange);
     var cell = sheet.getRange("D" + destrange);
     cell.setFormula('=Form!$C' + destrange);
     var cell = sheet.getRange("E" + destrange);
     cell.setFormula('=Form!$D' + destrange);
     var cell = sheet.getRange("F" + destrange);
     cell.setFormula('=Form!$E' + destrange);
     var cell = sheet.getRange("G" + destrange);
     cell.setFormula('=Form!$F' + destrange);
     var cell = sheet.getRange("H" + destrange);
     cell.setFormula('=Form!$G' + destrange);
    }

2 个答案:

答案 0 :(得分:1)

您需要做的就是将工作表变量设置为您要在

中写入的工作表

例如:在下面的代码中:

var sheet = range.getSheet()

获取名为&#34的工作表; Sheet2"你会这样做

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet2")

要在新工作表上获得空行,您将执行以下操作

 var destRange = sheet.getLastRow()+1 

getSheetByName上的文档可以是found here

另请注意,您一次编辑一个单元格的方法并不是最有效的方法 请查看此问题,了解如何批量更新Google app script timeout ~ 5 minutes?

编辑:

最终代码:

function onEdit(e){
    var ui = SpreadsheetApp.getUi(); 
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var range = e.range;
    var sheet = range.getSheet()
    var newSheet = ss.getSheetByName("Sheet2")
    var sheetName = sheet.getName()
    if(sheetName != "Form Responses"){
       return                            //exit function
    }  
    var columnOfCellEdited = range.getColumn();
    if (columnOfCellEdited === 1) 
     var range2 = range.getRow();
     var destrange = newSheet.getLastRow()
     sheet = newSheet
     // adds the formulas
     var cell = sheet.getRange("C" + destrange);
  cell.setFormula('=IFERROR(MID($B' + destrange +',SEARCH(":",$B' + destrange +')+1,SEARCH(",",$B' + destrange +')-SEARCH(":",$B' + destrange +')-1),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 :(得分:1)

@minky_boodle可以通过转到:enter image description here

来设置触发器

然后设置它: enter image description here