简体   繁体   English

如何使用应用脚本在GoogleSpreadSheet中的单元格中写入内容?

[英]How to write in cells in GoogleSpreadSheet using app Script?

and sorry for the stupid question. 对这个愚蠢的问题感到抱歉。

I know how to program on VBA for Excel, but I´m having a hard time doing the simplest stuff in Google Spreadsheet and I can´t find any good tutorials on-line. 我知道如何在VBA for Excel上编程,但是我很难在Google Spreadsheet中做最简单的事情,而且我找不到任何在线的好教程。

My question is regarding cells. 我的问题是关于细胞的。 In VBA to write in a cells I would do: 在VBA中写入单元格,我会这样做:

cells(1,1) = 2 //that would write the number 2 in the first row of the first column  

In VBA I can also assign a cell to a variable, like: 在VBA中,我还可以将单元格分配给变量,例如:

dim something as long
something = cells(1,1)

How can I apply the same principles in google SpreadSheet? 如何在Google SpreadSheet中应用相同的原则?

Thank you very much! 非常感谢你!

I´ve tried what you guys suggested. 我尝试了你们的建议。 And now I have a follow up question. 现在我有一个后续问题。

I`m trying to modify the cells in a specific range. 我正在尝试在特定范围内修改单元格。 I´ve tried this code: 我尝试了以下代码:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var board = sheet.getRange(2,2,9,9);
board[0][0].setValue(2);

But apparently I can´t use array notation. 但是显然我不能使用数组符号。 I´m not sure if .setValeu or .getRange are the problem. 我不确定.setValeu或.getRange是问题所在。 But is there a way I can use array notation to chance a cell? 但是,有没有一种方法可以使用数组表示法来对单元格进行赋值?

Thanks Again! 再次感谢!

Try following: 请尝试以下操作:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cell = sheet.getRange(1,1);
cell.setValue(2);

It's worthwhile looking at the docs for getRange . 值得看一下getRange文档

getRange(row, column, numRows, numColumns) getRange(行,列,numRows,numColumns)

Returns the range with the top left cell at the given coordinates with the given number of rows and columns. 返回具有给定的行数和列数的给定坐标处左上角单元格的范围。

You can therefore update multiple cells with an array like this: 因此,您可以使用以下数组更新多个单元格:

var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, numRows, numColumns).setValues(data);
SpreadsheetApp.flush();

暂无
暂无

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

相关问题 如何使用 AppScript 项目在 GoogleSpreadsheet 中设置单元格背景颜色 - How to set Cell Background Color in GoogleSpreadsheet Using AppScript Project 如何使用谷歌表格中的应用程序脚本在单元格中插入自动数据? - How to inset automatic data in Cells using app script in google sheet? Google App Script - 根据星期几写入单元格的脚本 - Google App Script - Script to write in cells according to the day of the week 将数据写入谷歌应用脚本中的多个非连续单元格 - Write data to multiple non-consecutive cells in google app script 计算应用脚本中的单元格以在谷歌表格中使用 - calculate cells in app script to using in google sheet 如何使用应用脚本删除特定的单元格? - How to delete specific cells with app script? 如何使用应用程序脚本在 Google 电子表格的单元格内添加 UI? - How do you add UI inside cells in a google spreadsheet using app script? 如何使用应用脚本更改 Google 电子表格单元格文本? - How can I change a Google spreadsheet cells text using app script? 如何使用 getLastRow() & shift 删除谷歌应用程序脚本中列范围内的单元格 - How to delete cells in a range of a column in google app script using getLastRow() & shift 如何制作谷歌应用程序脚本 - 日历 createEvent() 以接受来自使用数组公式填充的单元格的 GSheet 输入 - How to make google app script - calendar createEvent() to accept GSheet inputs from cells populated using array formula
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM