Google-sheets – Switch between different Tab´s

google sheetsgoogle-apps-scriptjavascript

I have a problem.

I need some values from Spreadsheet2 (Tab2), in Spreadsheet1 (Tab1).

Tab1 and Tab2:

I don´t know the URL from each Spreadsheet, they are unknown.

How can I switch between these Tabs and get the values from Spreadsheet2?

Does there exists a command for Google Apps Script?

Best Answer

Your script can be resolved with a couple of simple changes in syntax

  • getValue(); - you should assign this result to a variable so that you can use the variable to the setValue() on the other spreadsheet

  • getValue Vs getValues - getValue returns a single value (the value of the top-left cell in the range); by comparison getValues returns the rectangular grid of values for the range. Even though you specified the range as "A2:G10" by using getValue you only returned the value in cell A2. If you wanted the values in every cell of the range you should have used getValues

  • setValue Vs setValues - the same rule works for setting values - setValue sets a single value, and setvalues sets a rectangular grid of values (and must match dimensions of getValues range).
  • setValue(sourceValue) - you need to specify the value(s) that are to be set. This is the reason why you assign the getValue method to a variable. That variable is used here to define the value that is to be set.

  var sourcess = SpreadsheetApp.openById("1kEYM2FLO1x3LlRyTjQgZJHPNSoV_bhWSRWzu435fsRo");
  var sourceValue = sourcess.getSheetByName("Design Solution").getRange('A2').getValue();
  var targetss = SpreadsheetApp.openById("1YlEEfukZVD0k0nMBhLRmvCXbbKMsxB4lXy4d1PL4wFI");
  targetss.getSheetByName("Tabelle1").getRange('A2').setValue(sourceValue);