在Google Script中传输单元格数据? [英] Transferring cell data in Google Script?

查看:93
本文介绍了在Google Script中传输单元格数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为我的Google电子表格制作一个脚本。



我想要的是让脚本检查一个表格中的单元格是否为空,如果不是,我希望它将这些数据转移到另一张纸上。每当我尝试这个时,它总是说不能调用方法getRange()的未定义。



这是我到目前为止:

 函数timesheets(){
var ss = SpreadsheetApp.openById(1iL2Didv_YOLpTad5WdR3kZSjLOLTSQk_yV-gQhh4RXs);
if(ss!C2!=){
var Tasks = ss.getRange(C2)。getValue();




$ b我试图创建一个新函数,而不是使用Google工作表上的功能。我必须包含这两个电子表格的名称吗?

您正在访问电子表格,您需要了解我建议让脚本在同一个电子表格中工作,然后再尝试从一个到另一个进行交谈。

 函数timesheets(){
var ss = SpreadsheetApp.openById(1iL2Didv_YOLpTad5WdR3kZSjLOLTSQk_yV-gQhh4RXs);
var sheet = ss.getActiveSheet();
if(sheet!C2!=){
var Tasks = sheet.getRange(C2)。getValue();





我正在尝试创建新功能,而不是使用
Google表单上的功能。我必须包含
电子表格的名称吗?


包含代码的电子表格(目标)需要知道它正在查找的数据(来源)的表单ID。
当您想将值写入(目标)电子表格时,您需要给它一个写入的变量;

  var sheetTarger = SpreadsheetApp.getActiveSpreadsheet()。getActiveSheet(); 



SECTION UNDERNEATH是POST-EDIT ##



<我创建了两个电子表格。




  • 一个叫做源代码的单词hello b
  • 在一个叫做target的地方,我放置了下面的代码:

      function timesheets(){
    var ss = SpreadsheetApp.openById(*********************);
    var sheet = ss.getActiveSheet();
    if(sheet!C2!=){
    var Tasks = sheet.getRange(C2)。getValue();
    }
    var sheetTargert = SpreadsheetApp.getActiveSpreadsheet()。getActiveSheet();
    sheetTargert.getRange(C2)。setValue(Tasks);
    }




strong> ***************** 与源代码中的Id。



在脚本编辑器中,我运行函数timesheets()



值hello出现在目标单元格C2



编辑我去了

查看>执行记录



这产生了以下内容:

  [14-08-13 15:28:09:405 BST]开始执行
[14-08-13 15:28:09:787 BST] SpreadsheetApp.openById([**************])[0.366秒]
[14-08-13 15:28:09:891 BST] Spreadsheet.getActiveSheet( )[0秒]
[14-08-13 15:28:09:975 BST] Sheet.getRange([C2])[0.083秒]
[14-08-13 15:28: 10:058 BST] Range.getValue()[0.082 seconds]
[14-08-13 15:28:10:058 BST] SpreadsheetApp.getActiveSpreadsheet()[0 seconds]
[14-08 -13 15:28:10:139 BST] Spreadsheet.getActiveSheet()[0.08秒]
[14-08-13 15:28:10:139 BST] Sheet.getRange([10, 10] [0秒]
[14-08-13 15:28:10:140 BST] Range.setValue([hello])[0秒]
[14-08-13 15: 28. 10:386 BST] Sheet.getRange([C2])[0.245秒]
[14-08-13 15:28:10:3​​87 BST] Range.setValue([hello])[0 seconds]
[14-08-13 15:28:10:559 BST]执行成功[总运行时间0.967秒]


I'm trying to make a script for my Google spreadsheet.

What I want is for the script to check if a cell is empty in one sheet, and if its not, I want it to transfer that data into the cell of another sheet. Whenever I try this, it keeps saying "Cannot call method "getRange()" of undefined."

This is all I got so far:

function timesheets(){
  var ss = SpreadsheetApp.openById("1iL2Didv_YOLpTad5WdR3kZSjLOLTSQk_yV-gQhh4RXs");
  if("ss!C2" != ""){
    var Tasks = ss.getRange("C2").getValue();
  }
}

I'm trying to create a new function instead of using the functions on Google sheets. Would I have to include the names of both the spreadsheets?

解决方案

You are accessing a spreadsheet, you need to get down to the sheet level first.

I suggest getting the script to work within the same spreadsheet before you try to talk from one to another.

function timesheets(){
  var ss = SpreadsheetApp.openById("1iL2Didv_YOLpTad5WdR3kZSjLOLTSQk_yV-gQhh4RXs");
  var sheet = ss.getActiveSheet();
  if("sheet!C2" != ""){
    var Tasks = sheet.getRange("C2").getValue();
  }
}

I'm trying to create a new function instead of using the functions on Google sheets. Would I have to include the names of both the spreadsheets?

The spreadsheet that has the code in it (target) needs to know the Id of the sheet that it is looking at for data (source). When you want to write the value to the (target) spreadsheet, you need to give it a variable to write to;

var sheetTarger = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

SECTION UNDERNEATH IS POST-EDIT ##

I created two spreadsheets.

  • One called source where I wrote the word "hello" into cell C2
  • In one called "target" I placed the following code

    function timesheets(){
      var ss = SpreadsheetApp.openById("*********************");
      var sheet = ss.getActiveSheet();
      if("sheet!C2" != ""){
        var Tasks = sheet.getRange("C2").getValue();
      }
      var sheetTargert = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      sheetTargert.getRange("C2").setValue(Tasks);
    }
    

I replaced "*****************" with the Id from source.

Within the script editor I ran the function timesheets()

The value "hello" appeared in the target cell C2

In the script editor I went to

View>Execution transcripts

This produced the following

[14-08-13 15:28:09:405 BST] Starting execution
[14-08-13 15:28:09:787 BST] SpreadsheetApp.openById([**************]) [0.366 seconds]
[14-08-13 15:28:09:891 BST] Spreadsheet.getActiveSheet() [0 seconds]
[14-08-13 15:28:09:975 BST] Sheet.getRange([C2]) [0.083 seconds]
[14-08-13 15:28:10:058 BST] Range.getValue() [0.082 seconds]
[14-08-13 15:28:10:058 BST] SpreadsheetApp.getActiveSpreadsheet() [0 seconds]
[14-08-13 15:28:10:139 BST] Spreadsheet.getActiveSheet() [0.08 seconds]
[14-08-13 15:28:10:139 BST] Sheet.getRange([10, 10]) [0 seconds]
[14-08-13 15:28:10:140 BST] Range.setValue([hello]) [0 seconds]
[14-08-13 15:28:10:386 BST] Sheet.getRange([C2]) [0.245 seconds]
[14-08-13 15:28:10:387 BST] Range.setValue([hello]) [0 seconds]
[14-08-13 15:28:10:559 BST] Execution succeeded [0.967 seconds total runtime]

这篇关于在Google Script中传输单元格数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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