簡體   English   中英

我可以將數據從一個電子表格復制到新的電子表格嗎?

[英]Can I copy data from one spreadsheet to a new spreadsheet?

我目前正在開發一個腳本,以便在根據特定的單元格值更新單元格時自動發送電子郵件。 現在我想采用與我通過電子郵件發送的相同值,並將它們填充到新的電子表格中,並發布該行更新時間的時間戳。 在新的SS中,它將是A(時間戳),B,C,D,E(BE將是自動通過電子郵件發送的數據)。

在這里的另一個用戶的幫助下,我能夠使自動電子郵件系統正常工作。 我發現有幾個帖子提出了類似的問題,我試圖拼湊起來看看我是否可以開始工作,但我沒有成功。 我一直在代碼中遇到障礙,因為我不知道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得到更新。 除了目前向我發送信息的電子郵件之外。 還要將所有信息更新到完全不同的電子表格中的新行,其中包含編輯時間的時間戳,以便我可以記錄它。

在我對你所做的事情的有限理解中,我得到了這個。

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' );
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM