简体   繁体   English

使用Google脚本将数据从一个Google电子表格拉到另一电子表格

[英]Pull data from one Google spreadsheet to another using Google script

I have 2 spreadsheet in my Drive. 我的云端硬盘中有2个电子表格。 I want to pull data from a cell in 1 spreadsheet and copy it in another. 我想从1个电子表格的单元格中提取数据,然后将其复制到另一个电子表格中。 The spreadsheet "TestUsage" will sometimes have data in column A, but none is column B. I would like so that when I open the spreadsheet, it would populate that empty cell in sheet "TestUsage" from sheet "TestParts". 电子表格“ TestUsage”有时会在A列中包含数据,但B列中没有。在我打开电子表格时,它将在工作表“ TestParts”中的工作表“ TestUsage”中填充该空白单元格。 Here is my code: 这是我的代码:

var ssTestUsage = SpreadsheetApp.getActiveSpreadsheet();
var sTestUsage = ssTestUsage.getActiveSheet();
var lastRowTestUsage = sTestUsage.getLastRow();
var rangeTestUsage = sTestUsage.getSheetValues(1, 1, lastRowTestUsage, 4);
var TESTPARTS_ID = "1NjaFo0Y_MR2uvwit1WuNeRfc7JCOyukaKZhuraWNmKo";
var ssTestParts = SpreadsheetApp.openById(TESTPARTS_ID);
var sTestParts = ssTestParts.getSheets()[0];
var lastRowTestParts = sTestParts.getLastRow();
var rangeTestParts = sTestParts.getSheetValues(1, 1, lastRowTestParts, 3);

function onOpen() {
  for (i = 2; i < lastRowTestUsage; i++) {
    if (rangeTestUsage[i][0] !== "" && rangeTestUsage[i][1] == "") {
      for (j = 1; j < lastRowTestParts; j++) {
        if (rangeTestUsage[i][0] == rangeTestParts[j][0]) {
          Logger.log(rangeTestUsage[i][1]);
          Logger.log(rangeTestParts[j][1]);
          rangeTestUsage[i][1] = rangeTestParts[j][1];
          break;
        }
      }
    }
  }
}

The problem with this is this doesn't do anything: 这个问题是什么都不做:

rangeTestUsage[i][1] = rangeTestParts[j][1];

I know there must be a method that can get data from one range to another. 我知道必须有一种方法可以从一个范围到另一个范围获取数据。 Please let me know if I am totally incorrect or I am on the right path. 如果我完全不正确,或者我走的路正确,请告诉我。

the statement 该声明

"this doesn't do anything:" “这没有做任何事情:”

rangeTestUsage[i][1] = rangeTestParts[j][2];

is not really true... and not really false neither..., actually it does assign the value to rangeTestUsage i but you dont see it because it is not reflected in the spreadsheet. 不是真的,也不是真的...,实际上它确实将值分配给rangeTestUsage i,但是您没有看到它,因为它没有反映在电子表格中。

Both values are taken from the Sheet using getValues so at that time they are both array elements. 这两个值都是使用getValues从Sheet中获取的,因此当时它们都是数组元素。

What is missing is just writing back the array to the sheet using the symetrical statement setValues() 缺少的只是使用对称语句setValues()将数组写回到工作表

Give it a try and don't hesitate to come back if something goes wrong. 尝试一下,如果出现问题,请立即返回。

EDIT : 编辑:

I didn't notice at first that you were using getSheetValues instead of getValues (simply because I never use this one)... the only difference is that getValues is a method of the range class while yours belongs to the sheet class ; 起初我没有注意到您使用的是getSheetValues而不是getValues (这是因为我从未使用过它)……唯一的区别是getValues是range类的一种方法,而您的属于sheet类 the syntax is similar in a way, just use 语法在某种程度上相似,只是使用

Sheet.getRange(row,col,width,height).getValues()

it takes one word more but has the advantage to have a direct equivalent to set values 它需要多花一个字,但具有直接等于设定值的优点

Sheet.getRange(row,col,width,height).setValues()

Serge insas has a good explanation of why your code doesn't work and hints at the solution below. Serge insas很好地解释了为什么您的代码不起作用,并提示了以下解决方案。

I recommend you use an array to store the updated values of column B that you want then set the entire column at the end. 我建议您使用一个数组来存储所需的列B的更新值,然后在末尾设置整个列。

Modifying your code... 修改您的代码...

var ssTestUsage = SpreadsheetApp.getActiveSpreadsheet();
var sTestUsage = ssTestUsage.getActiveSheet();
var lastRowTestUsage = sTestUsage.getLastRow();
var rangeTestUsage = sTestUsage.getSheetValues(1, 1, lastRowTestUsage, 2);
var TESTPARTS_ID = "1NjaFo0Y_MR2uvwit1WuNeRfc7JCOyukaKZhuraWNmKo";
var ssTestParts = SpreadsheetApp.openById(TESTPARTS_ID);
var sTestParts = ssTestParts.getSheets()[0];
var lastRowTestParts = sTestParts.getLastRow();
var rangeTestParts = sTestParts.getSheetValues(1, 1, lastRowTestParts, 2);

var colB = [];

function onOpen() {
  for (i = 2; i < lastRowTestUsage; i++) {
    if (rangeTestUsage[i][0] !== "" && rangeTestUsage[i][1] == "") {
      var matched = false;
      for (j = 1; j < lastRowTestParts; j++) {
        if (rangeTestUsage[i][0] == rangeTestParts[j][0]) {
          //Logger.log(rangeTestUsage[i][1]);
          //Logger.log(rangeTestParts[j][1]);
          colB.push([rangeTestParts[j][1]]); // push the value we want into colB array
          matched = true;
          break;
        }
      }
      if(!matched) // this is in case you don't have a match
        colB.push([""]); // incase we don't have a matching part
    } else {
      colB.push([rangeTestUsage[i][0]]); // we already have a value we want so just add that to colB array
    }
  }
  sTestUsage.getRange(2,2,lastRowTestUsage).setValues(colB); // update entire column b with values in colB array
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Apps脚本:如何从一个电子表格中提取今天输入的数据并将其转换为另一电子表格? - Google Apps Script: How do I pull data entered today from one spreadsheet and transpose it to another? 使用 Google Apps 脚本将数据范围从一个 Google 电子表格导出到另一个 - Export Range of Data from One Google Spreadsheet to Another using Google Apps Script 如何使用谷歌应用脚本将一行从一个谷歌电子表格复制到另一个谷歌电子表格? - How to copy a row from one google spreadsheet to another google spreadsheet using google apps script? 在 Google Apps 脚本中从另一个电子表格编辑一个电子表格 - Editing one Spreadsheet from another Spreadsheet in Google Apps Script 使用 Google Apps 脚本将一个 Google 电子表格中的特定数据复制到另一个 Google 电子表格 - Copy Specific Data in one Google Spreadsheet to another Google Spreadsheet with Google Apps Script 如何在不使用电子表格 ID 的情况下使用 Google 脚本从另一个 Google 表格导入数据 - How to import data from another Google Sheet using Google Script without using Spreadsheet ID 如何从谷歌电子表格中提取数据到网站? - How to pull data from google spreadsheet to a website? 将过滤后的范围从一个电子表格复制到另一个 - Google 应用脚本 - Copy filtered range from one spreadsheet to another - Google app script 如何使用 Google App Script 将一个 Google Sheet 中的所有数据提取到另一个 Google Sheet Tab? - How do I pull All data in one Google Sheet to another Google Sheet Tab using Google App Script? 如何使用Google Apps脚本将特定的行从3个Google电子表格复制到另一个电子表格 - How to copy specific rows from 3 google spreadsheet to another spreadsheet using google apps script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM