谷歌脚本:访问电子表格文件,TypeError:无法调用方法“openById”的空值。 (第2行,文件“代码”)关闭 [英] Google Scripts: accessing spreadsheet files, TypeError: Cannot call method "openById" of null. (line 2, file "Code") Dismiss

查看:158
本文介绍了谷歌脚本:访问电子表格文件,TypeError:无法调用方法“openById”的空值。 (第2行,文件“代码”)关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

  function myFunction(){
var sheet = SpreadsheetApp.getActiveSpreadsheet()。openById 'https://docs.google.com/a/e4b.us/spreadsheets/d/1DBty8ZiTqZ8MHPkFipjL8wDZqR2Ae9PLrNAQtW2QD2k/edit#gid=0');
var ss = SpreadsheetApp.getActive();
var mylocationInfo = ss.getRange(A2:B4)。getValues();
logger.log(mylocationInfo.getData)

}



我认为可能会删除网址的非必要部分,例如/ edit#gid = 0。它没有。



以下证据显示该网址确实有效:



编辑我试过了,没有整个网址,只是使用实际的ID并收到相同的消息。



编辑我删除了函数.getActiveSpreadSheet并得到错误Bad值
我试着改变getById getByUrl,输入完整的URL,并且我得到错误信息,缺少ID的文件。如屏幕截图所示,文件存在。该URL在执行脚本之前立即被复制粘贴。
已更改:3:07pm

编辑我尝试使用getByUrl链接到不同的电子表格,并得到相同的错误消息。改变3:10 pm

为什么getById返回Null?
为什么getByUrl找不到我的文件?

解决方案

您正在使用: getActiveSpreadsheet ) openById()。这是一个冲突。您需要使用其中一种。



您的代码:

  var sheet = SpreadsheetApp.getActiveSpreadsheet()
.openById('https://docs.google.com/a/e4b.us/spreadsheets/d/Your ID / edit#gid = 0');
var ss = SpreadsheetApp.getActive();

另外,您并未使用该ID,而是使用该网址。如果您想通过URL获得对文件的引用,请使用以下方法: openByUrl()



应该看起来像这样:

  var ss = SpreadsheetApp.openById('Your ID'); 
var sheet = ss.getActiveSheet();

变量名称 ss 代表 pread s heet。这是整个电子表格文件。 工作表是电子表格中的一个工作表选项卡。


The code:

function myFunction() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().openById('https://docs.google.com/a/e4b.us/spreadsheets/d/1DBty8ZiTqZ8MHPkFipjL8wDZqR2Ae9PLrNAQtW2QD2k/edit#gid=0');
var ss = SpreadsheetApp.getActive();
var mylocationInfo   = ss.getRange("A2:B4").getValues();
logger.log(mylocationInfo.getData)

}

I thought that maybe removing non-essential aspects of the url, like the /edit#gid=0, would help. It didn't.

Here's evidence that the URL is indeed valid:

EDIT I've tried it without the entire URL, just using the actual ID and I receive the same message.

EDIT I removed the function ".getActiveSpreadSheet" and get the error "Bad Value" I tried changing getById to getByUrl, inputing the full URL, and i get the error message that the file with the ID is missing. As was shown by the screenshot, the file exists. The url was copy-pasted immediately before executing the script. changed:3:07pm

EDIT I tried linking to a different spreadsheet using getByUrl and got the same error message. Changed 3:10pm

Why does getById return Null? Why can't getByUrl find my files?

解决方案

You are using: getActiveSpreadsheet() and openById() at the same time. That's a conflict. You need to use one or the other.

Your code:

var sheet = SpreadsheetApp.getActiveSpreadsheet()
  .openById('https://docs.google.com/a/e4b.us/spreadsheets/d/Your ID/edit#gid=0');
  var ss = SpreadsheetApp.getActive();

Plus you aren't using the ID, you are using the URL. If you want to get a reference to the file by URL, use the method: openByUrl()

The code should look like this:

var ss = SpreadsheetApp.openById('Your ID');
var sheet = ss.getActiveSheet();

The variable name ss stands for spread sheet. That's the entire spreadsheet file. sheet is one sheet tab inside the spreadsheet.

这篇关于谷歌脚本:访问电子表格文件,TypeError:无法调用方法“openById”的空值。 (第2行,文件“代码”)关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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