简体   繁体   中英

Google Scripts, how to set data into sheet with variable name

My goal is to set a cell on another sheet to a specific value. The sheet and the cell required are variable. I have stored in rows M & F on the active sheet both the required row and the sheet required.

SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Navigation").getRange('B1').setValue("Test1");

The code above works, setting the value "Test1" to cell B1 on the Navigation Sheet.

However since the sheet and the cell have to be variables based on data stored within cells, I had to do:

function onEdit(e) {
  // Set a comment on the edited cell to indicate when it was changed.
  var sheet = SpreadsheetApp.getActiveSheet();
  var Range = e.range;
  var Row = e.getrange.rowStart;
  Logger.log(Row);
 
  if(range.getSheet().getSheetName() == "Summary"&& //Ensure trigger on single column in document
    e.range.columnStart == 10 &&  // This section works
    e.range.columnEnd == 10 &&
    e.range.rowStart >= 4 &&
    e.range.rowEnd <= 5000) {
    
        // range.setValue(Row);
        Range1 = sheet.getRange("M1:M100"); //Obtain the row for the desired cell (stored within cells in column M)
        var cell1 = Range1.getCell(Row,1);
        var Row_Value = cell1.getValue();
        // range.setValue(Row_Value);

        Range2 = sheet.getRange("F1:F100"); //Get the name of the desired  sheet (stored within cells in column F)
        var cell2 = Range2.getCell(Row,1);
        var Name_Value = cell2.getValue();
        // range.setValue(Name_Value);

        Range3 = sheet.getRange("A1:G200");  //Get the Cell Location as a range
        var cell3 = Range3.getCell(Row_Value,7);

        Range4 = sheet.getRange(cell3);
        SpreadsheetApp.getActiveSpreadsheet().getSheetByName(Name_Value).getRange(Row_Value).setValue("Test1"); //Set value to required range on required sheet.
    }
}

This however does not work. What it should do is take the required row from column M and the sheet name from column F and use that to write "Test" to that cell on that sheet.

I do get an error during debugging

TypeError: Cannot read property 'range' of undefined onEdit

Which I attribute to the fact that the function requires a range (e) but isn't given one during debugging.

If anyone could help remedy the problem that would be great, as I am clueless when it comes to javascript. Thank you - S

Try this:

function onEdit(e) {
  const sheet = e.range.getSheet();
  if (sheet.getName() !== 'Summary'
    || e.range.columnStart !== 10
    || e.range.rowStart < 4) {
    return;
  }
  const sheetName = sheet.getRange('F' + e.range.rowStart).getValue() || 'no sheet name';
  const columnLetter = sheet.getRange('G' + e.range.rowStart).getValue() || 'no column letter';
  const rowNumber = sheet.getRange('M' + e.range.rowStart).getValue() || NaN;
  const targetCell = e.source.getRange(sheetName + '!' + columnLetter + rowNumber);
  targetCell.setValue('testing...');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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