簡體   English   中英

Google 表格腳本多個 onEdit 更改

[英]Google Sheets Script multiple onEdit changes

示例電子表格的鏈接位於此處。

我已經能夠單獨完成以下所有 3 件事:

  1. 寫入第 4 列時,將時間戳添加到 Column1 中。
  2. 當 checkbox=TRUE 時,將時間戳添加到 Column8 中。
  3. 當 checkbox=FALSE 時清除第 8 列的內容。

問題是我使用 onEdit 一次只能獲得一個 function。 有人可以告訴我我做錯了什么嗎? 我真的希望所有 3 個方面的功能同時工作。 下面是我嘗試合並的代碼片段示例。

function onEdit(e) {
        if(e.value != "TRUE" ) return;
    e.source.getActiveSheet().getRange(e.range.rowStart,e.range.columnStart+2).setValue(new Date());
}
function onEdit(e)
{ 
  var sheet = e.source.getActiveSheet();
  if (sheet.getName() == "order data") //"order data" is the name of the sheet where you want to run this script.
  {
    var actRng = sheet.getActiveRange();
    var editColumn = actRng.getColumn();
    var rowIndex = actRng.getRowIndex();
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
    var dateCol = headers[0].indexOf("Date") + 1;
    var orderCol = headers[0].indexOf("Order") + 1;
    if (dateCol > 0 && rowIndex > 1 && editColumn == orderCol) 
    { 
      sheet.getRange(rowIndex, dateCol).setValue(Utilities.formatDate(new Date(), "UTC+8", "MM-dd-yyyy")); 
    }
  }
}```
if( r.getColumn() == 16 ) { 
    var nextCell = r.offset(0, 1);
    if (r.getValue() == 1) {
        if( nextCell.getValue() === '' ) 
            nextCell.setValue(new Date()).setNumberFormat("MM-dd-yyyy hh:mm");
    } else {
        nextCell.setValue('');
    }
}```

謝謝大家的任何建議,可以幫助我克服這個問題。

嘗試

function onEdit(e){
  onEdit1(e)
  onEdit2(e)
  onEdit3(e)
}

並將 onEdit1、onEdit2、onEdit3 賦予您的個人功能

function onEdit(e){
  onEdit1(e)
  onEdit2(e)
  onEdit3(e)
}
function onEdit1(e){
  Browser.msgBox('first onEdit function')
}
function onEdit2(e){
  Browser.msgBox('second onEdit function')
}
function onEdit3(e){
  Browser.msgBox('third onEdit function')
}

假設所有個人 function 都正常工作

暫無
暫無

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

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