当列被更新/更改时如何添加一行?

huangapple go评论83阅读模式
英文:

How to add a row when a column is updated/changed?

问题

I have translated the code part for you:

function addRowOnEdit(e) {
  var sheet = e.source.getActiveSheet();
  var range = e.range;

  // Define the trigger range
  var triggerRange = sheet.getRange('M2:M');

  // Check if the edited cell is within the trigger range
  if (range.getColumn() === triggerRange.getColumn() && range.getRow() >= triggerRange.getRow() && range.getRow() <= triggerRange.getLastRow()) {
    // Get the last row of the sheet
    var lastRow = sheet.getLastRow();

    // Insert a new row at the last row
    sheet.insertRowAfter(lastRow);

    // You can also add data to the newly added row if needed
    var newRow = sheet.getRange(lastRow + 1, 1); // Adjust the column number if needed
    newRow.setValue("New Data"); // Change "New Data" to the desired value
  }
}

If you have any more text to translate or any questions, please let me know.

英文:

I have 2 sheets (Form Responses 1 & Sheet 2), the first one is receiving data from a google forms while the second references the first to do some calculations.

I'm using

=SUMIFS(&#39;Form responses 1&#39;!C2:C;M2:M;month(A9))

to sum all the values in a given month in Sheet 2.

The M column is an array formula that updates the column with the months of every entry from Form Responses 1

Whenever a new entry is sent through the form, Form Responses 1 adds a new row to the sheet. SUMIFS does not work if both sheets are not the same size.

I need to make so that whenever the M column is updated in Sheet 2, it automatically adds 1 row so that it keeps the same size as Form Responses 1 as it gets new data added

I tried using a google script that has a trigger onChange

function addRowOnEdit(e) {
  var sheet = e.source.getActiveSheet();
  var range = e.range;
  
  // Define the trigger range
  var triggerRange = sheet.getRange(&#39;M2:M&#39;);
  
  // Check if the edited cell is within the trigger range
  if (range.getColumn() === triggerRange.getColumn() &amp;&amp; range.getRow() &gt;= triggerRange.getRow() &amp;&amp; range.getRow() &lt;= triggerRange.getLastRow()) {
    // Get the last row of the sheet
    var lastRow = sheet.getLastRow();
    
    // Insert a new row at the last row
    sheet.insertRowAfter(lastRow);
    
    // You can also add data to the newly added row if needed
    var newRow = sheet.getRange(lastRow + 1, 1); // Adjust the column number if needed
    newRow.setValue(&quot;New Data&quot;); // Change &quot;New Data&quot; to the desired value
  }
}

答案1

得分: 0

尝试在SUMIF中使用该数组公式了吗?不仅要有列M,还要将它与SUMIF一起使用,但不使用它进行SUMIF。

=SUMIFS('Form responses 1'!C2:C;ARRAYFORMULA(MONTH('Form responses 1'!A2:A));month(A9))

英文:

Have you tried using that array formula inside the SUMIF? Instead of having column M, or along with having it but without using it for your SUMIF

=SUMIFS(&#39;Form responses 1&#39;!C2:C;ARRAYFORMULA(MONTH(&#39;Form responses 1&#39;!A2:A));month(A9))

huangapple
  • 本文由 发表于 2023年8月5日 01:11:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定