无法在Google脚本中选择文档正文

时间:2018-08-06 14:05:30

标签: google-apps-script google-docs

我正在尝试将文本打印到使用Google Apps脚本创建的google doc中,并且具有以下代码:

DocumentApp.openByUrl("https://docs.google.com/document/d/1YPthft_5n5TFpnN2uqwonHtxcFmI40LOoVei7N1IdXA/edit");
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();

var x = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", 
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
function CreateDoc(name) {
  DocumentApp.create(name);
}
function rightClick(document) {
  var i
  for(i = 0; i < 26; i++) {

    body.appendParagraph("x[i]");
  }
}
rightClick();

发生的事情是,当我运行任何命令时,都会出现以下错误: TypeError: Cannot call method "getBody" of null. (line 3, file "Code") 我不明白为什么要这么做或如何解决。如果您能帮助我修复它,那就太好了。

1 个答案:

答案 0 :(得分:0)

使用openByUrl打开文档不会使其成为活动文档。

将第一行更改为

begin_date_time=datenum('2011-01-06 00:00:00','yyyy-mm-dd HH:MM:SS');
str_next_date=strcat('2011-01-06',32,'00:15:00');
next_date_time=datenum(str_next_date,'yyyy-mm-dd HH:MM:SS');
interval=next_date_time-begin_date_time;
end_date_time=datenum('2011-12-31 00:00:00','yyyy-mm-dd HH:MM:SS');
full_data_time_steps=begin_date_time:interval:end_date_time;

删除第二行。