我可以将数据从一个电子表格复制到新电子表格吗?

时间:2019-06-03 16:29:45

标签: javascript google-apps-script google-sheets

我目前正在编写一个脚本,以便在根据单元格的特定值更新单元格时自动发送电子邮件。现在,我想采用与我通过电子邮件发送的相同值,并将它们填充到新的电子表格中,并发布该行何时更新的时间戳。在新的SS中,它将是A(时间戳),B,C,D,E(B-E将是自动通过电子邮件发送的数据)。

在这里的另一个用户的帮助下,我能够使自动电子邮件系统正常工作。我发现有几条帖子问类似的问题,我试图将它们拼凑起来,看看是否可以解决这个问题,但没有成功。我一直遇到代码块,因为我不知道google-apps-scripts的所有正确语法。我尝试了copyTo(),duplicateSheet()。但是问题是我只关心一次更新一行,因为该行上的特定单元格被更新了。我发现的都是关于将整个数据表复制到新电子表格中的帖子。我以为我必须在上面定义它,然后在if语句中添加copyTo(),但是每次我尝试它时,if语句都将中断,并在代码中得到错误消息。

(我在项目触发器中也有一个onEdit触发器)

function sendNotification(e){ 

  var ss = e.source.getSheetByName('Datasheet'); //defines the source of 
where to look
  var cell = e.range.getA1Notation(); 
  var row = e.range.getRow(); //from the range gets the row(is important 
for calling the control owner and control ID on line 15 and 16)
  var col = e.range.getColumn(); //from the range gets the 
column(important when pulling specific columns of information)
  var cellvalue = e.range.getValue(); //this pulls whatever is inside of 
the cell. (so 'NAME' in column i)
  var subject = 'SUBJECT: '+ ss.getSheetName(); //tells the program what 
to put in the subject line of the email (ss.getSheetName() gets the name 
 of the tab of the data)
  var name = ss.getRange(row, 9).getValue(); //get column 9 for current 
row (column 9 is column i which is the control certifier)



  if (name === 'NAME' && (col === 23 || col === 24 || col === 31 || col 
=== 32) === true) {  //states: if the cell  in column i = TRUE, AND column 
(w)23, (x)24, (ae)31 OR (af)32 = changed/updated THEN execute command 
below
   var control = ss.getRange(row, 2).getValue(); //get value for column B 
in updated cell row
   var owner = ss.getRange(row, 8).getValue();   //get value for column H 
in updated cell row





    //line 21-35 is the email formatting
    //MailApp.sendEmail() sends the email if line 14 = TRUE
    // to: who the email is sent too
    //Subject = subject defined above
   //htmlBody = what is in the body of the paragraph sent


   MailApp.sendEmail({
      to: "EMAIL",
      subject: subject,
      htmlBody: "The following cell has been updated: <br><br>"+
      "<br><br>" + "The control is: " + control +
      ", <br><br>The owner is: " + owner +  
      "<br><br>The change was: " + cellvalue + "<br>" + 
      "<br><br>Thank you. <br><br><br><br>" +
        })
  }
}

基本上,如果列W,X,AE或AF更新时,我会喜欢它。给我发送一封有关该信息的电子邮件,这是当前工作的重中之重。还要将所有信息更新到完全不同的电子表格的新行中,并带有编辑时间的时间戳记,以便我进行记录。

1 个答案:

答案 0 :(得分:0)

在让您对自己的工作了解有限的情况下,我做到了这一点。

function notify(e){ 
  var sh=e.range.getSheet();
  var dsh=e.source.getSheetByName('Sheet161');
  var name=sh.getRange(e.range.rowStart,9).getValue();
  var col=e.range.columnStart;
  //e.source.toast('Start');
  if (sh.getRange(e.range.rowStart,9).getValue()=='NAME' && sh.getName()=='Selection') {
    //e.source.toast('Flag1');
    if (col==23 || col==24 || col==31 || col==32) {
      //e.source.toast('Flag2'  +  sh.getRange(e.range.rowStart,col).getValue());
      if(sh.getRange(e.range.rowStart,col).getValue()==true) {
        //e.source.toast('Flag3');
        var control=dsh.getRange(e.range.rowStart, 2).getValue();
        var owner=dsh.getRange(e.range.rowStart, 8).getValue();
      }
    }
    var html="The following cell has been updated: <br><br>"; 
    html+="The control is: " + control + ", <br><br>The owner is: ";
    html+=owner + "<br><br>The change was: " + e.value + "<br>" + "<br><br>Thank you. <br><br><br><br>";
    //MailApp.sendEmail({to: "EMAIL",subject: subject,htmlBody:html});
    var userInterface=HtmlService.createHtmlOutput(html);
    SpreadsheetApp.getUi().showModelessDialog(userInterface,'data' );
  }
}