简体   繁体   English

Google Scripts,如何将数据设置为具有变量名的工作表

[英]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.

相关问题 如何从 Google App Scripts 中的另一个工作表中提取数据验证 - How to pull data validation from another sheet in Google App Scripts 创建新的电子表格,以表格 Google Scripts 的名称命名 - Create new spreadsheet name it by the name of sheet Google Scripts Google脚本-如何调用正确的表格 - Google Scripts - How to call the correct sheet 如何使用 jQuery 从选项卡中获取名称并将名称数据从主机表发送到名称表?-App 脚本 - How grab names from tab with jQuery & send name data from host sheet to Name's sheet?-App Scripts 如何使用Google表格脚本将Google表格单元格数据添加到html电子邮件中? - How do you add google sheet cell data into an html-email using Google Scripts for Sheets? 如何使用Google脚本将数据和格式从Google表格A复制到现有表格B而不在日期上添加时间戳 - How to copy data and formatting from google sheet a to existing sheet b using google scripts without adding timestamp to dates Google表格脚本 - Google Sheet scripts 如何在 Google Sheet 上的 Apps Scripts 中使用 google API? - How to use google API in Apps Scripts on Google Sheet? 尝试将工作表行数据发送到GmailApp时缺少变量名; Javascript / Google App脚本 - Missing variable name while trying to send sheet row data to GmailApp; Javascript/Google App script 将变量(不是表单提交)中的数据发送到谷歌表 - Send data in variable (not on form submit) to google sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM