I am trying to insert a timestamp from one spreadsheet to another spreadsheet

I have two spreadsheets, we will call them Active Spreadsheet (where the code is being implemented) and Target Spreadsheet (where the timestamps go) The Active Spreadsheet will have data entered on sheet “Week1” into column D starting at row 4. When the data is entered into column D I want an “Initial Date” timestamp and a “Modified Date” timestamp populated into the Target Spreadsheet, sheet “Week#1” at the corresponding rows in columns 1 and 2 respectively. I have the following code written, and it runs without errors. However, when I input data into column D of the Active Spreadsheet, nothing populates in the Target Spreadsheet. I have tried running the code and researching various reasons as to why it is not working but cannot find a solution.

    function onEdit(e){
     const row = e.range.getRow();
     const col = e.range.getColumn();
     const sheetName = "Week1";

      if (col === 4 && row > 3 && e.source.getActiveSheet().getName() === "Week1"){

       var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
       var targetSpreadsheetId = "TargetSpreadsheetID";
       var targetSpreadsheet = SpreadsheetApp.openById(targetSpreadsheetId);
       var targetSheet = targetSpreadsheet.getSheetByName("Week#1");
       var targetRow = range.getRow();
       var timestamp = new Date();
       var formattedTimestamp = Utilities.formatDate(timestamp, Session.getScriptTimeZone(), "yyyy-MM-dd HH:mm:ss");

      targetSheet.getRange(row,2).setValue(formattedTimestamp);

       if(targetSheet.getRange(row,1).getValue() == ""){
        targetSheet.getRange(row,1).setValue(formattedTimestamp);

    }
  }
}

New contributor

Brian Mueller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

SUGGESTED SOLUTION

Note: It seems like onEdit(e) won’t work for this because accessing another spreadsheet requires authorization, and as per Restrictions:

They can modify the file they are bound to, but cannot access other files because that would require authorization.

Instead, use Manage triggers manually to run the function. Click on Triggers > + Add Trigger on the left of the Google Apps Script Editor.

SAMPLE TRIGGER IMAGE

I changed the name of the function from onEdit(e) to myFunction so that the triggers will not run twice. This is an example of what it’ll look like if that is not changed:

The converted code would become:

function myFunction() {
  // Variables from the original code
  var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var targetSpreadsheetId = "TargetSpreadsheetID";
  var targetSpreadsheet = SpreadsheetApp.openById(targetSpreadsheetId);
  var targetSheet = targetSpreadsheet.getSheetByName("Week#1");
  var timestamp = new Date();
  var formattedTimestamp = Utilities.formatDate(timestamp, Session.getScriptTimeZone(), "yyyy-MM-dd HH:mm:ss");

  // Newly declared variables
  var sh = activeSpreadsheet.getSheetByName("Week1");
  var rg = sh.getActiveRange();
  var row = rg.getRow();
  var col = rg.getColumn();
  var vl = sh.getRange(row, col).getValue();

  // Conditional statement that checks if there's already a timestamp in Columns A and B 
  if (col == 4 && vl != "" && row > 3) {
    var cv = targetSheet.getRange(row, 1).getValue();
    if (cv != "") {
      targetSheet.getRange(row, 2).setValue(formattedTimestamp);
    } else {
      targetSheet.getRange(row, 1).setValue(formattedTimestamp);
    }
  }
}

OUTPUT

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật