[英]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.
我已将所需行和所需工作表都存储在活动工作表上的 M 和 F 行中。
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Navigation").getRange('B1').setValue("Test1");
The code above works, setting the value "Test1" to cell B1 on the Navigation Sheet.上面的代码有效,将值“Test1”设置为导航表上的单元格 B1。
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.
它应该做的是从列 M 中获取所需的行,并从列 F 中获取工作表名称,并使用它将“测试”写入该工作表上的该单元格。
I do get an error during debugging我在调试期间确实收到错误
TypeError: Cannot read property 'range' of undefined onEdit
TypeError:无法读取未定义 onEdit 的属性“范围”
Which I attribute to the fact that the function requires a range (e) but isn't given one during debugging.我将其归因于 function 需要一个范围 (e) 但在调试过程中没有给出这一事实。
If anyone could help remedy the problem that would be great, as I am clueless when it comes to javascript.如果有人可以帮助解决这个问题,那就太好了,因为我对 javascript 一无所知。 Thank you - S
谢谢你-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...');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.