简体   繁体   English

尝试使用应用程序脚本制作基本的谷歌表格功能

[英]Trying to make a basic google sheets function with apps script

Here's the code:这是代码:

 function calculateJuniorCredit() { var app = SpreadsheetApp; var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); for (var i = 2; i <= 45; i++) { var classNumber = spreadsheet.getRange(i, 3); var juniorCredit = spreadsheet.getRange(4, 12).setValue(0); if (classNumber >= 100 && classNumber < 200) { juniorCredit.setValue(spreadsheet.getRange(i, 5) + juniorCredit); } } }

The current issue is that it says that .getRange(i,3) is null, even though there's an integer value in the cell, that being said I'm not very familiar with JS syntax or Apps Script so there's probably other issues.当前的问题是它说 .getRange(i,3) 为空,即使单元格中有一个整数值,据说我对 JS 语法或 Apps 脚本不是很熟悉,所以可能还有其他问题。 What I'm trying to do is add together all the credits of my 100 level (junior) classes and display the value in cell L4 or (4,12).我想要做的是将我的 100 级(初级)课程的所有学分加在一起,并在单元格 L4 或 (4,12) 中显示该值。 The class number is found in column C or 3 from rows 2 to 45, and the credit value is in column E or 5.班级编号位于 C 或 3 列的第 2 至 45 行,学分值位于 E 或 5 列。

Google App Script has multiple .getRange methods. Google App Script 有多个 .getRange 方法。 It appears the one you are looking for is under the Sheet Class, not Spreadsheet.您正在寻找的似乎是在 Sheet Class 下,而不是 Spreadsheet 下。

Once you change to the active sheet, then you'll get the proper method with the row and column numbers.更改为活动工作表后,您将获得带有行号和列号的正确方法。 You can access the Sheet class by adding on the getActiveSheet method您可以通过添加 getActiveSheet 方法来访问 Sheet 类

Consider updating your spreadsheet variable:考虑更新您的电子表格变量:

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

Here is the documentation for the Sheet.getRange method 这是 Sheet.getRange 方法的文档

Once you open up the documentation, you'll notice that the return type is Range .打开文档后,您会注意到返回类型是Range

That's important because your classNumber variable is a Range, not a number.这很重要,因为您的classNumber变量是一个范围,而不是一个数字。 So you're checking if the range is equal to 100-199.所以你要检查范围是否等于 100-199。 That conditional will never succeed.那个条件永远不会成功。

I assume you're meaning to check if the value is equal to any value from 100-199.我假设您想检查该值是否等于 100-199 之间的任何值。 You might consider using the Range.getValue method similar to the Range.setValue method which you're already using.您可以考虑使用Range.getValue方法,类似于您已经在使用的 Range.setValue 方法。

I'm hoping this points you in the right direction.我希望这会为您指明正确的方向。 I don't want to write all the code for ya ;)我不想为你写所有的代码;)

The following line has no meaning and serves no purpose.下面这行没有任何意义,也没有任何意义。

var app = SpreadsheetApp;

The following line gets you the entire file - the spreadsheet.以下行为您提供整个文件 - 电子表格。 It does not define which tab to use.它没有定义要使用的选项卡。

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();

You can fix it the way 8ryan8 suggested, by getting the active tab:您可以按照 8ryan8 建议的方式修复它,方法是获取活动选项卡:

var activeSheet = spreadsheet.getActiveSheet();

Or you can get the tab by name:或者您可以按名称获取选项卡:

var activeSheet = spreadsheet.getSheetByName('Name_of_your_sheet');

The following line gets the cell (i, 3) .以下行获取单元格(i, 3) It does not get the value in the cell:它没有获取单元格中的值:

var classNumber = spreadsheet.getRange(i, 3);

To get the value, use:要获取该值,请使用:

var classNumber = activeSheet.getRange(i, 3).getValue();

The logic of the rest of the code does not make sense.其余代码的逻辑没有意义。 The following line gets the cell L4.下面一行获取单元格 L4。 And sets the value of L4 to 0.并将 L4 的值设置为 0。

var juniorCredit = spreadsheet.getRange(4, 12).setValue(0);

But juniorCredit is not a value but a cell.juniorCredit不是一个值,而是一个单元格。

And spreadsheet.getRange(i, 5) is a cell, not the value in the cell.并且spreadsheet.getRange(i, 5)是一个单元格,而不是单元格中的值。

So in the following won't work:所以在下面将不起作用:

juniorCredit.setValue(spreadsheet.getRange(i, 5) + juniorCredit);

I'm not clear on what you are trying to accomplish.我不清楚你想要完成什么。 You'll need to rethink the logic here.你需要重新思考这里的逻辑。

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

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