经过三天的试验和搜索,我仍然无法使.getValue函数正常工作

时间:2019-02-28 15:09:51

标签: function

很抱歉,主题行很长,但是此网站不接受更简洁的声明。

我有一个Google表格,其中只有我在单元格A1中的名字。

然后我转到工具/脚本编辑器,并显示一个“ Code.gs”脚本:

function onEdit(e) {
  Logger.log("We're in the function.");

  var ss = Spreadsheet.App.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var valueOfCell = sheet.getRange(1,1).getDisplayValue();
  Logger.log("Value of Cell is '" + valueOfCell + "'.");
}

当我编辑非常简单的工作表并检查日志(“查看” /“日志”)时,我得到:

[19-02-28 08:58:10:182 CST] We're in the function.

就是这样。经过三天的网络搜索,我尝试了所有可以想到的或可疑的排列方式,但是我无法使.getValue()(或.getDisplayValue())正常工作。

我在做什么错了?

谢谢!

/肯特

2 个答案:

答案 0 :(得分:0)

“ Spreadsheet.App”应为“ SpreadsheetApp”。

有效的其他排列:

Logger.log("We're in the function.");
var ss = SpreadsheetApp.getActiveSheet();
var valueOfCell = ss.getRange(1,1).getDisplayValue;

Logger.log("We're in the function.");
var valueOfCell = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(1,2).getDisplayValue();

Logger.log("We're in the function.");
var valueOfCell = SpreadsheetApp.getActiveSheet().getRange(1,2).getDisplayValue();

答案 1 :(得分:0)

四天后,我想我终于明白了一些。对于像我这样密集的人,我将本文档留在这里:

电子表格是“工作簿”,是 Sheet 标签的集合 Sheet Spreadsheet “工作簿”中单个制表符的工作表。

似乎足够简单的信息,但是我找不到任何地方记录的信息。

SpreadsheetApp 是类,是所有电子表格的“父级”。尽管从技术上讲可能不准确,但出于我的想法,我认为“ SpreadsheetApp”是Google Docs中的“ Sheets”应用程序,而不是“ Docs”或“ Slides”。

所以

var ThisApp = SpreadsheetApp;
// Sets the variable "ThisApp" to "SpreadsheetApp", which I guess is the name/identification of the spreadsheet app.

var TheActiveWorkBook = SpreadsheetApp.getActive();
// Sets "TheActiveWorkBook" to the identifier for the current collection of sheets (collection of tabs within a spreadsheet).

var TheActiveWorkBook = SpreadsheetApp.getActiveSpreadsheet();
// Identical to above; just a different name for it.

var ActiveWorkBookName = SpreadsheetApp.getActive().getName();
// Sets the variable to the *name* of the workbook/spreadsheet (like, "Fred's Spreadsheet").

var ActiveSheetInWB = SpreadsheetApp.getActiveSpreadsheet();getActiveSheet();
// Sets the variable to the identifier of the active tab in the spreadsheet.

var ActiveSheetInWB = SpreadsheetApp.getActive();getActiveSheet();
// Identical to above.

var ActiveSheetInWB = SpreadsheetApp.getActiveSheet();
// Identical to above. The active sheet is the active sheet, regardless of whether we know the active spreadsheet, so the extra step of including the active spreadsheet, as we did in the previous two examples, is unnecessary. (The system knows that the active spreadsheet is the one that contains the active sheet.)

var ActiveSheetInWB = SpreadsheetApp.getActiveSheet().getName();
// Gets the *name* of the sheet, as it shows on that sheet's tab.

var sheet = SpreadsheetApp.getActiveSheet();
var ActiveSheetInWB = sheet.getName();
// Shortens the long "SpreadsheetApp.getActiveSheet()" to a short reference to save typing in subsequent uses. Otherwise identical to above example.

我想我终于明白了。