简体   繁体   English

谷歌电子表格 - 制作按钮,按下时将数据添加到另一个工作表中的单元格中

[英]Google spreadsheet - Making buttons that add data into cells into another sheet when pressed

I'm trying to make spreadsheets to keep track of moods and other things.我正在尝试制作电子表格来跟踪情绪和其他事情。 What I want to do is to have a dashboard with buttons with differnt moods that, when pressed, add a value and a specific color to a cell in a second sheet.我想要做的是有一个带有不同情绪按钮的仪表板,当按下这些按钮时,会在第二张表中的单元格中添加一个值和特定颜色。 This second sheet will function as a simple database and has the first column with the date (365 rows), and another column with the mood with the colored cells that will be added every time you press the button.第二张表格将 function 作为一个简单的数据库,第一列是日期(365 行),另一列是带有颜色的单元格的心情,每次按下按钮时都会添加。

Also, the value when you press the button with the selected mood, must be added next to the row that has the current date.此外,当您按下带有所选心情的按钮时的值,必须添加到具有当前日期的行旁边。

Summing up:加起来:

  • I want to make buttons in one sheet我想在一张纸上制作按钮

  • When you press thos buttons, a specific value is added into another sheet当您按下这些按钮时,特定值会添加到另一个工作表中

  • That value must be added next to the cell with the current date, in the column with all the dates该值必须添加到具有当前日期的单元格旁边,在包含所有日期的列中

I've searched this here, but can't find anything similar.我在这里搜索过这个,但找不到类似的东西。 If you can help me directly or posting links to the subject I'll be thankfull.如果您可以直接帮助我或发布该主题的链接,我将不胜感激。

What you are describing is in fact quite easy to do with Sheets and Google Apps Scripts.您所描述的实际上很容易使用表格和 Google Apps 脚本来完成。 These are the steps:这些是步骤:

  1. Create a Sheets document.创建表格文档。
  2. Create a sheet named database .创建一个名为database的工作表。 Set the headers (ie A1=Date , B1=Mood ).设置标题(即A1=DateB1=Mood )。
  3. Go to Tools > Script Editor. Go 到工具 > 脚本编辑器。 Paste the following code and save the project:粘贴以下代码并保存项目:
var TIMEZONE = "GMT";

function wasInserted(date) {
  var lastInsertedDate = PropertiesService.getScriptProperties().getProperty('lastDate');
  return date == lastInsertedDate;
}

function getDateNow() {
  return Utilities.formatDate(new Date(), TIMEZONE, "M/d/y");
}

function insertMood(mood) {
  var date = getDateNow();
  if (wasInserted(date)) return; // mood already inserted for the day.

  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('database');
  ss.appendRow([date, mood]);
  PropertiesService.getScriptProperties().setProperty('lastDate', date);
}

function selectHappy() {
  insertMood('happy');
}

function selectConfused() {
 insertMood('confused');
}

function selectSad() {
 insertMood('sad');
}
  1. Create three images in the main sheet of your Sheets document ( important : they must be "Image over cells").在表格文档的主工作表中创建三个图像(重要:它们必须是“单元格上的图像”)。 They must map to Happy, Confused, and Sad, which are the moods that i defined for the application.他们必须 map 到快乐、困惑和悲伤,这是我为应用程序定义的情绪。 Of course, you may modify this and add as many moods as you please.当然,您可以修改它并添加任意数量的情绪。

  2. Click on each of the pictures, click the three dots and click on "Assign script".单击每张图片,单击三个点,然后单击“分配脚本”。 For the happy picture, it should be "selectHappy" (see above code - it must map the function names), and so on.对于快乐的图片,它应该是“selectHappy”(参见上面的代码 - 它必须是 map function 名称),等等。

Now, go and test it.现在,go 并对其进行测试。 click any of the pictures.单击任何图片。 The first time it will ask you for permissions, which you must accept.第一次它会要求您提供权限,您必须接受。 Afterwards, you won't be required to accept them.之后,您将无需接受它们。 Each time you click them a new entry will be created in the "database" sheet with the selected mood.每次单击它们时,都会在“数据库”表中创建一个带有所选心情的新条目。 The buttons can only be clicked once a day, the second time (and so on) will not do anything.按钮每天只能点击一次,第二次(以此类推)不会做任何事情。

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

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