谷歌工作表脚本获取单元格内容

时间:2016-10-23 20:48:36

标签: json parsing spreadsheet

我在google spreadhseet中有一个网址列表,我希望将该单元格引用用作脚本中的URL。

我将从以下网址获取数据的示例:https://politicsandwar.com/api/nation/id=31831

List of URLs

我想做一些像getValue。(" O2")或var urlData =" O2"但我不知道如何去达到这一点。

1 个答案:

答案 0 :(得分:1)

首先应该获得一个范围,然后获取其值。

var sheet = SpreadsheetApp.getActiveSheet();

//Gets all cell values form column "O" starting from row 2. Returns a 2d array
var wholeColumnUrls = SpreadsheetApp.getActiveSheet().getRange("O2:O").getValues();

//Gets value from specific cell "O2".
var singleCellUrl = SpreadsheetApp.getActiveSheet().getRange("O2").getValue();

multipleways defining a range,在此示例中我们使用A1notation variant。这似乎是你正在寻找的东西。

另请注意getValues()getValue()之间的区别。第一个返回2d数组,如:

[
    [https://politicsandwar.com/api/nation/id=31831]
    ,[https://politicsandwar.com/api/nation/id=31832]
    ,[https://politicsandwar.com/api/nation/id=31833]
]

而后者返回单个值,如https://politicsandwar.com/api/nation/id=31831

通过这种方式,您可以选择获取所有值并迭代它们,或者使用您自己的逻辑来确定每个单元格并单独获取值。

相关问题