cedjones
cedjones

Reputation: 3

Pull data from one Google spreadsheet to another using Google script

I have 2 spreadsheet in my Drive. I want to pull data from a cell in 1 spreadsheet and copy it in another. 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". 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.

Upvotes: 0

Views: 4104

Answers (2)

Gene
Gene

Reputation: 114

Serge insas has a good explanation of why your code doesn't work and hints at the solution below.

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.

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
}

Upvotes: 0

Serge insas
Serge insas

Reputation: 46822

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 rangeTestUsagei but you dont see it because it is not reflected in the spreadsheet.

Both values are taken from the Sheet using getValues so at that time they are both array elements.

What is missing is just writing back the array to the sheet using the symetrical statement 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; 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()

Upvotes: 1

Related Questions