简体   繁体   English

Google 电子表格脚本编辑器:onEdit setValue 或 clearContent

[英]Google spreadsheets script editor: onEdit setValue or clearContent

Im trying to make a spreadsheet to manage my money/expenses and have run into a problem trying to automate my process我试图制作一个电子表格来管理我的钱/开支,但在尝试自动化我的流程时遇到了问题

I want to make a piece of code that runs every time a cell has been edited in the sheet.我想制作一段代码,每次在工作表中编辑单元格时都会运行。

When triggered, i want it to calculate ssum, lsum and betal(in the loop), and then put it into 3 different cells.触发时,我希望它计算 ssum、lsum 和 betal(在循环中),然后将其放入 3 个不同的单元格中。 The code behaves as expected, but the onedit trigger doesnt work.代码按预期运行,但 onedit 触发器不起作用。

This is my code:这是我的代码:

function regnudbetalprocent() {

  var betal = 0;
  var i = 1;
  var app = SpreadsheetApp;
  var ss = app.getActiveSpreadsheet();
  var activeSheet = ss.getActiveSheet();
  var sum = activeSheet.getRange(18, 5).getValue();
  var ssum;
  var lsum;
  var ssumori = activeSheet.getRange(3, 8).getValue();
  var lsumori = activeSheet.getRange(4, 8).getValue();
  var fuld = activeSheet.getRange(18, 2).getValue();

  while(betal < sum){

    ssum = ((ssumori - fuld / 2) / 100) * i;
    lsum = ((lsumori - fuld / 2) / 100) * i;
    betal = ssum + lsum;

    i++;
  }


    if (betal > sum) {
      var output = [
    [ssum,lsum],
    ["Samlet",betal]
  ]
       return output;
    }
}

The output variable sets the neighbouring cells accordingly from where the function is called输出变量从调用函数的位置相应地设置相邻单元格

I've tried with setValue and clearContent, but it i cant edit outside the cell from where the function is called.我已经尝试过 setValue 和 clearContent,但我无法在调用函数的单元格外进行编辑。 I've used Edit -> current project's triggers to add an onEdit trigger, which increments each time i edit the sheet, but nothing happens.. I'm burned out我已经使用 Edit -> current project's triggers 添加了一个 onEdit 触发器,每次我编辑工作表时它都会增加,但没有任何反应......我已经筋疲力尽了

Can someone guide me?有人可以指导我吗? how do i get what i want?我如何得到我想要的?

  • The values for using with the calculation are the constant cells of "B18", "E18", "H3" and "H4".用于计算的值为“B18”、“E18”、“H3”和“H4”的常数单元格。
  • You want to run the script when one of above cells is edited.您希望在编辑上述单元格之一时运行脚本。 You want to use OnEdit event trigger.您要使用 OnEdit 事件触发器。
  • You want to put the result value to the cells of "D21", "E21" and "E22".您想将结果值放入“D21”、“E21”和“E22”的单元格中。
  • Your calculation has no issues.你的计算没有问题。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several possible answers.请将此视为几种可能的答案之一。

Modification points:改装要点:

  • In your script, I think that the simple trigger can be used instead of the installable trigger.在您的脚本中,我认为可以使用简单触发器代替可安装触发器。
  • From your replying, about "Exception: You do not have permission to call setValue (line 7)."从您的回复中可以看出, "Exception: You do not have permission to call setValue (line 7)." , I think that you might be using the function of regnudbetalprocent() as the custom function. ,我认为您可能正在使用regnudbetalprocent()的函数作为自定义函数。 In this case, such error occurs.在这种情况下,会发生这样的错误。
  • You can know the coordinate of the edited cell using the event object.您可以使用事件对象知道编辑单元格的坐标。

When above points are reflected to your script, it becomes as follows.当以上几点反映到你的脚本中时,它变成如下。

Modified script:修改后的脚本:

The function of onEdit is used as the simple trigger. onEdit函数用作简单触发器。 So in order to run the script, please manually edit one of cells "B18", "E18", "H3" and "H4".因此,为了运行脚本,请手动编辑单元格“B18”、“E18”、“H3”和“H4”之一。 By this, the script is run and retrieved values from the cells of "B18", "E18", "H3" and "H4", and the calculated result is put to the cells of "D21:E22".这样,脚本运行并从“B18”、“E18”、“H3”和“H4”的单元格中检索值,并将计算结果放入“D21:E22”的单元格中。

function onEdit(e) {
  var a1Notation = e.range.getA1Notation();  // Added
  if (a1Notation != "B18" && a1Notation != "E18" && a1Notation != "H3" && a1Notation != "H4") return;  // Added

  var betal = 0;
  var i = 1;
  var app = SpreadsheetApp;
  var ss = app.getActiveSpreadsheet();
  var activeSheet = ss.getActiveSheet();
  var sum = activeSheet.getRange(18, 5).getValue();
  var ssum;
  var lsum;
  var ssumori = activeSheet.getRange(3, 8).getValue();
  var lsumori = activeSheet.getRange(4, 8).getValue();
  var fuld = activeSheet.getRange(18, 2).getValue();
  while(betal < sum){
    ssum = ((ssumori - fuld / 2) / 100) * i;
    lsum = ((lsumori - fuld / 2) / 100) * i;
    betal = ssum + lsum;
    i++;
  }

  if (betal > sum) {  // Modified
    var output = [[ssum,lsum], ["Samlet",betal]];
    var r = e.range.getSheet().getRange("D21:E22");
    r.clearContent();  // This line might not be required.
    r.setValues(output);
    SpreadsheetApp.flush();  // This line might not be required.
  }
}

References:参考:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM