简体   繁体   中英

Google script write one cell to range i nanother Spreadsheet

pulling my hair a bit here.

I'm trying to build a "database-like" structure on Google Sheets (modify entry in one document, it gets updated elsewhere).

  • 2 spreadsheets, need to move data from one to another
  • select SINGLE CELL on "Spreadsheet1", copy to a RANGE of cells on "Spreadsheet2"
  • I got past many errors and fixed them but this one is invincible:

Range not found (line 10, file "Code")

What am I doing wrong?

function ImportDataRange() {

  var activeSs = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = activeSs.getSheetByName("PRODUCT dashboard");
  var ssOrigin = SpreadsheetApp.openById("1gF-IsnPdnjAaoUSwOOaBfWTmK1eVzxDG87XPzjjsWxA");
  var sheetOrigin = ssOrigin.getSheetByName("INITIAL_BUDGETS");

  for (var i = 7; i < 26; i++) {

    var rangeOrigin = sheetOrigin.getRange("i, 1");
    var dataOrigin = rangeOrigin.getValues();

    for (var i = 10; i < 275; i += 6) {
      sheet.getRange("i, 1").setValues(dataOrigin);
    }
  }

} 

You probably got it but I need to: copy A7 from document1 into 15 cells of document2, than take cell A8 form document1 copy into next 15 cells of document2, then take A9.... You get it.

Here's how I would do it:

function ImportDataRange() {

  var activeSs = SpreadsheetApp.getActiveSpreadsheet(),
      sheet = activeSs.getSheetByName("PRODUCT dashboard"),
      rangeSheet = sheet.getRange(10, 1, 265), // 3rd number is the number of rows to get into the range, as described in documentation
      valsSheet = rangeSheet.getValues(); //Get all values in a double array, this is to keep any previous values in the sheet, also it already gets the array just as it is needed to use in setValues()
      ssOrigin = SpreadsheetApp.openById("1gF-IsnPdnjAaoUSwOOaBfWTmK1eVzxDG87XPzjjsWxA"),
      valsOrigin = ssOrigin.getRange(7, 1, 15).getValues(), // Same as the other getRange, gets 15 lines, starting in line 7
      sheetOrigin = ssOrigin.getSheetByName("INITIAL_BUDGETS");

  for ( origins in valsOrigin  ) { // For each value in the valsOrigin array, do something -> in each loop origins will be the next index, Eg. 0, then 1, then 2...
    for (var i = 10; i < 275; i += 6) {
      i = (+i) + (+origins); // Add the origins so it don't begin in the same line, altough it will start to overwrite after 6 loops, you should look into this logic - also, the '+' converts it to number, as they can be strings
      valsSheet[i][0] = valsOrigin[origins][0]; // Puts the current value of origins in the target array
    }
  }
  rangeSheet.setValues( valsSheet ); // Inserts all values at once in the range
}

Minimized SpreadsheetApp calls, those are the costly one in time, which is limited in GAS, also makes for a cleaner code.

Get rid of your quotes in the getRange() parameters, you're not using a string. Just need .getRange(i, 0) .

Also, if you're just getting one cell as your range and one setting value, you can use .setValue() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM