简体   繁体   English

我不明白为什么我会收到此错误

[英]I don't understand why I am getting this error

I am completely unfamiliar with JavaScript but I am trying to create a script that makes a formatted timestamp in a specific cell when a button is pressed.我对 JavaScript 完全不熟悉,但我正在尝试创建一个脚本,在按下按钮时在特定单元格中生成格式化的时间戳。

I have gotten it to work with a true/false in sheets however I don't know how to make this in JavaScript.我已经让它在工作表中使用真/假,但是我不知道如何在 JavaScript 中进行此操作。 This is as far as I have gotten so far and I'm already hitting errors I do not understand.这是我到目前为止所获得的,我已经遇到了我不明白的错误。

Can someone help me understand or link me resources on how to figure out some of these problems?有人可以帮助我理解或链接我有关如何解决其中一些问题的资源吗? I've spent several hours googling and reading Google developer pages trying to learn but I haven't had any luck thus far.我花了几个小时在谷歌上搜索和阅读谷歌开发者页面试图学习,但到目前为止我还没有运气。

The error:错误:

在此处输入图像描述

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSheet()
  var date = new Date(); var timeStamp = date.getTime(); // Unix Timestamp
  var currentTime = date.toLocaleTimeString(); // eg. 10:23:30 AM HKT 
  sheet.setValue(currentTime);
}

You need to set the value on a cell, not the entire sheet.您需要在一个单元格上设置值,而不是整个工作表。 To do it on the cell at the very top left, you would select A1 for example:要在最左上角的单元格上执行此操作,您可以使用 select A1例如:

function myFunction() {
  var date = new Date();
  var timeStamp = date.getTime(); // Unix Timestamp
  var currentTime = date.toLocaleTimeString(); // eg. 10:23:30 AM HKT 

  var sheet = SpreadsheetApp.getActiveSheet(); // Sheet
  var cell = sheet.getRange('A1'); // Cell           <------------------------
  cell.setValue(currentTime); // <--------------------------------------------
}

While the accepted answer gives you a completely valid solution, I thought I would walk you through my own process for figuring this out pretty quickly using the documentation (without necessarily having any previous experience with google apps scripts).虽然接受的答案为您提供了一个完全有效的解决方案,但我想我会引导您完成我自己的过程,以便使用文档快速解决这个问题(不一定有任何以前的谷歌应用程序脚本经验)。

The SpreadsheetApp.getActiveSheet() method returns the active Sheet object. SpreadsheetApp.getActiveSheet()方法返回活动工作object。

We can browse the docs for the Sheet class to see which methods are available and which might be useful to us.我们可以浏览Sheet class 的文档以查看哪些方法可用以及哪些可能对我们有用。

getActiveRange() looks interesting. getActiveRange()看起来很有趣。 It:它:

Returns the selected range in the active sheet, or null if there is no active range.返回活动工作表中的选定范围,如果没有活动范围,则返回 null。

So again, searching through the docs for the Range class to see what methods are available to the range class, we can find setValue .因此,再次通过文档搜索Range class 以查看范围 class 可用的方法,我们可以找到setValue

function myFunction() {
  var date = new Date();
  var timeStamp = date.getTime();
  var currentTime = date.toLocaleTimeString(); 

  var sheet = SpreadsheetApp.getActiveSheet();
  var activeRange = sheet.getActiveRange();
  if (activeRange) {
      activeRange.setValue(currentTime);
  }
}

Hopefully this answer teaches you a bit more of a step-by-step approach in figuring these kind of problems out in the future.希望这个答案能教会你更多一步一步的方法来解决未来的这类问题。

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

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