Question

In Google Sheets I have a sheet with Form Responses, and to the right of the form columns I have columns with formulas that use form data for functions.

At the start I had the formulas extended down the rows so they worked on new form submissions, but found that new form submissions would clear the row out :(.

Instead of manually extending formulas down after each submission I installed Andrew Stillman's copyDown() script; what it does is copies down the formulas after scripts are submitted.

Now the problem I'm having is that the script works when run manually, but when I set to trigger on form submits it copies the said formula on that sheet and on all other sheets in the spreadsheet. I Do Not Want that side-effect, as it messes the whole spreadsheet up. :((

What I thought to do is edit script so it only works on the one Form Response sheet, not all sheets. But I don't know how to do that.

The name of the sheet I want it to run on is "Requests", and the gid=8.

How do I edit this script to only work for that one sheet?

Was it helpful?

Solution

To get code to run on just a particular sheet use the .getSheetByName() method. For example:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var reqSh = ss.getSheetByName('Requests');

There is another way that might be easier. You could try keeping one sheet purely for form submissions and use an arrayformula in a second sheet to copy any values from the first sheet to the same range in the second.

=ARRAYFORMULA('Requests'!A1:H) would copy columns A to H.

I had the same problem as you and this was the solution. I placed my formulas in the second sheet in the columns to the right of the range and copied them down in the normal way. The formulas referred to the copied range in the second sheet. It worked a treat.

I didn't come up with the idea myself - I'm sure it was suggested by someone on the Google spreadsheet forum. I should give you a link to the post but I just looked and I can't find it.

OTHER TIPS

In your code you have (comments in code)

 var sheets = ss.getSheets() [8]; // you choose sheet [8]
 var cellAddresses = new Object();
  for (var i=0; i<sheets.length; i++) { // but you enter a for loop that adresses every sheet in turn...
    var range = sheets[i].getDataRange();

You should simply suppress this loop and use only the sheet number you want to be proceeded ...

The simplest way could be to do it like this :

 var i = 8
 var sheets = ss.getSheets() [i];
 var cellAddresses = new Object();
    var range = sheets[i].getDataRange();
  ...

and at the end of the loop delete the } that fitted the for loop

EDIT : the new code should be like this :

function copydown() {
  setCopyDownUid();
  setCopyDownSid();
  logCopyDown();
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets() [8];
  var cellAddresses = new Object();
  var i=8
//  for (var i=0; i<sheets.length; i++) {
    var range = sheets[i].getDataRange();
    var lastRow = range.getLastRow();
    var values = range.getValues();
    for (var j=0; j<values.length; j++) {
      for (var k=0; k<values[j].length; k++) {
        var test = values[j][k].toString();
        var start = test.indexOf("copydown");
          if (start == 0) {
            start = start+10;
            var end = test.length-2;
            var length = end-start;
            var value = test.substr(start, length);
            var col = k+1;
            var nextRow = j+2;
            var numRows = lastRow-(nextRow-1);
            if (numRows>0) {
              var destRange = sheets[i].getRange(nextRow, col, numRows, 1);
              destRange.clear();
              var newLastRow = sheets[i].getDataRange().getLastRow();
              var newNumRows = newLastRow-(nextRow-1);
              var newDestRange = sheets[i].getRange(nextRow, col, newNumRows, 1);
              var cell = sheets[i].getRange(nextRow-1, col);
              cell.setFormula(value);
              cell.copyTo(newDestRange);
            }
            var cellAddress = cell.getA1Notation();
            cellAddresses[cellAddress] = test;
          }
      }
    }
    Utilities.sleep(500);
    resetCellValues(cellAddresses, sheets[i]);
  }
//}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top