简体   繁体   English

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

[英]After Three Days of Experimenting and Searching, I STILL Can't Get .getValue Function to Work

Sorry for the long subject line, but this site wouldn't accept a more concise statement. 抱歉,主题行过长,但是此网站不接受更简洁的声明。

I have a Google Sheet, that has nothing more than my name in Cell A1. 我有一个Google表格,除了单元格A1中的名字外,没有其他内容。

Then I went to Tools / Script Editor, and have a "Code.gs" script that says: 然后,我转到“工具/脚本编辑器”,并显示一个“ 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 + "'.");
}

When I edit my very simple sheet, and check the log (View / Logs), I get: 当我编辑非常简单的工作表并检查日志(查看/日志)时,我得到:

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

That's it. 而已。 I've tried every permutation I can think of or suss from three days of web-searching, and I simply can not make the .getValue() (or .getDisplayValue()) work. 经过三天的网络搜索,我尝试了所有可以想到的或可疑的排列,但我根本无法使.getValue()(或.getDisplayValue())正常工作。

What am I doing wrong? 我究竟做错了什么?

Thanks! 谢谢!

/Kent /肯特

"Spreadsheet.App" should be "SpreadsheetApp". “ Spreadsheet.App”应为“ SpreadsheetApp”。

Additional permutations that work: 有效的其他排列:

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

and

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

and

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

After four days, I think I finally understand some of this. 四天后,我认为我终于了解了其中的一些内容。 For others as dense as I, I leave this documentation here: 对于像我一样密集的其他人,我将本文档留在这里:

Spreadsheet is the "workbook", the collection of Sheet tabs Sheet is the individual tab'd sheet in a Spreadsheet "workbook". Spreadsheet是“工作簿”,工作选项卡的集合工作Spreadsheet “工作簿”中各个选项卡式工作

Seems like simple enough information, but I couldn't find this documented anywhere. 似乎足够简单的信息,但是我找不到任何地方记录的信息。

SpreadsheetApp is the Class, the "parent" of all the Spreadsheets. SpreadsheetApp是类,是所有Spreadsheets的“父级”。 While probably not technically accurate, for purposes of my thinking, I think of "SpreadsheetApp" as being the "Sheets" application within Google Docs, as opposed to "Docs" or "Slides". 尽管从技术上讲可能不准确,但出于我的想法,我认为“ SpreadsheetApp”是Google Docs中的“ Sheets”应用程序,而不是“ Docs”或“ Slides”。

So, 所以,

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.

I think I'm finally getting the hang of this.... 我想我终于明白了这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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