简体   繁体   English

您如何使用“OnEdit”功能将谷歌表格宏设置为仅在一张纸上运行?

[英]How do you set a google sheets macro with the "OnEdit" function to run only on one sheet?

I'm currently trying to make a budget spending tracker that automatically populates a date next to each purchase I track.我目前正在尝试制作一个预算支出跟踪器,它会在我跟踪的每次购买旁边自动填充一个日期。 The setup is that on sheet 2, column c is where I add my expense, and column B is where the date should be added.设置是在工作表 2 上,c 列是我添加费用的位置,B 列是应添加日期的位置。 The code I am currently using is pasted below.我目前使用的代码粘贴在下面。

`function onEdit(e) {
  var row = e.range.getRow();
  var dateCell = e.range.getSheet().getRange(row, 2);
  if (!dateCell.getValue()) {
    dateCell.setValue(new Date()).setNumberFormat("MM/dd/YY");
  }
}`

This is working fine except for one thing, it is activating in every spreadsheet, not just spreadsheet 2. How can I setup my code so that it only runs when another column in spreadsheet 2 is changed?这工作正常,除了一件事,它在每个电子表格中激活,而不仅仅是电子表格 2。如何设置我的代码以便它仅在电子表格 2 中的另一列更改时运行?

Thank you谢谢

In order for your onEdit(e) trigger to work only on Sheet2 you should add an if condition where you check if the name of the sheet is the one that you want the trigger be executed on.为了让您的onEdit(e)触发器Sheet2上工作,您应该添加一个if条件,您可以在其中检查工作表的名称是否是您希望在其上执行触发器的名称。

Assuming that the sheet name is "Sheet2" , you can try this:假设工作表名称是"Sheet2" ,你可以试试这个:

function onEdit(e) {
    var sheet = e.range.getSheet();
    if (sheet.getName() === "Sheet2") {
        var row = e.range.getRow();
        var dateCell = sheet.getRange(row, 2);
        if (!dateCell.getValue()) {
            dateCell.setValue(new Date()).setNumberFormat("MM/dd/YY");
        }
    }
}

Reference参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在谷歌表格中 - 如何应用 onEdit 触发器以仅在一张表格上的一个单元格上运行? - In google sheets - how do I apply an onEdit trigger to run on just one cell on one sheet? 谷歌应用脚本 OnEdit 仅在一张表中的一个单元格上运行 - Google app script OnEdit to run only on one cell in one sheet 尝试运行多个 function onEdit - Google 表格 - Trying to run more than one function onEdit - Google Sheets google sheet 中的 onedit 脚本仅适用于一个 function - onedit script in google sheet works for only one function 在 Google 表格移动应用上使用 onEdit 运行 function - Run a function with onEdit on Google Sheets mobile app onEdit Google Sheet 脚本只在一张纸上运行? - onEdit Google Sheet script runs only on one sheet? 如何使用 onEdit(e) function 为 Google Spread Sheet 中的多个子表获取日期和时间 - How to get Date & Time using onEdit(e) function for multipal Sub Sheets in Google Spread Sheet 编辑其他工作表时如何在 Google 工作表中刷新过滤器 onEdit()? - How to refresh a filter onEdit() in Google sheets when editing a different sheet? 如何使用 Google Sheets onEdit() 触发器运行函数来更新 Google 表单 - How to use Google Sheets onEdit() trigger to run function to update Google Form Google 表格 OnEdit() - 您无权调用服务 - Google Sheets OnEdit() - You do not have permission to call a service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM