获取和设置单元格的值不起作用 [英] get and set Value of a Cell do not work

查看:27
本文介绍了获取和设置单元格的值不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从电子表格中的单元格获取值.我在文档中找到了 getCell() 方法,但它不起作用.它总是只得到范围"而不是值.(顺便说一句,它是一个整数.)而且我没有找到 setCell() 方法!o.O 值在单元格 B:1 中感谢您的帮助!!1 :)

I have trouble with getting a Value from a Cell in my Spreadsheet. I found the getCell()-method in the documentation but it does not work. It always just gets "Range" instead of the Value. (it is a integer btw.) And I don't find a setCell()-method ! o.O The Value is in the cell B:1 Thx for any help!!1 :)

var API_KEY           = "***",
    PROFILE_ID        = "****";
    GET_PLUS_ONES_URI = "https://www.googleapis.com/plus/v1/people/"+PROFILE_ID+"?fields=plusOneCount&key="+API_KEY;
var currentPlusOnes = 0,
    lastPlusOnes    = 0,
    ss              = SpreadsheetApp.getActive();

function execute() {
  getCurrentPlusOnes();
  getLastPlusOnes();
  if ( currentPlusOnes !== getLastPlusOnes ) {
    setCurrentValue();
  }
}

function getCurrentPlusOnes() {
  currentPlusOnes = JSON.parse( UrlFetchApp.fetch( GET_PLUS_ONES_URI )).plusOneCount;
}

function getLastPlusOnes() {
  lastPlusOnes = ss.getRange("B1").getCell( 1, 1 ); // --> Allways just "Range" instead of the value
}

function setCurrentValue() {
  ss.getDataRange().setValue( currentPlusOnes );
}

推荐答案

getRange()getCell() 都返回一个 Range 对象.您需要在某个范围内调用 getValue()getValues() 以获取数据.在这种情况下,由于您的第一个 getRange("B1") 仅引用单个单元格,因此您不需要 getCell(1,1)

both getRange() and getCell() return a Range object. You need to get call getValue() or getValues() on a range to get the data out. In this case as your first getRange("B1") is only referencing a single cell, you don't need the getCell(1,1)

lastPlusOnes = ss.getRange("B1").getValue();

https://developers.google.com/应用程序脚本/参考/电子表格/范围#getValue()

同样适用于设置.单个单元格中的单个值将是:

for setting the same applies. a single value in a single cell would be:

Range.setValue( number | string )

或者如果你有一堆行和列,那么首先选择合适的大小范围,然后使用 setValues([][])

or if you have a a bunch of rows and columns then first select the appropriate sized range and then use setValues([][])

这篇关于获取和设置单元格的值不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆