简体   繁体   中英

how to insert multiple rows in a spreadsheet using google script

Is there any way i can insert multiple Rows into Google SpreadSheet without using any loops.

Need to do this task using google Apps Script only.

 function testMultipleEntry(){ var sheet = SpreadsheetApp.openById(INSERT-SHEET-ID); var mainSpreadSheet= sheet.getActiveSheet(); var fiveRows= mainSpreadSheet.getRange(2, 1, 5,mainSpreadSheet.getMaxColumns() ); var tempSheet= SpreadsheetApp.create("TestSheetAppend").getActiveSheet(); fiveRows.copyValuesToRange(tempSheet, 1, mainSpreadSheet.getMaxColumns(), 2, 5);//used the array[][] }

last line is showing -- Service error: Spreadsheets

I'm still not clear on what it is you're looking for, but this code takes data from one sheet, and inserts it into another one:

function enterData(){

  var ss = SpreadsheetApp.openById('SHEET-ID');
  var mainSheet= ss.getActiveSheet();
  var fiveRows= mainSheet.getRange(2, 1, 5,mainSheet.getMaxColumns());
  var createSheet= ss.insertSheet("TestSheetAppend");//This line will fail if a sheet by this name exists. 
  var tempSheet = ss.getSheetByName('TestSheetAppend');

  fiveRows.copyValuesToRange(tempSheet, 1, tempSheet.getMaxColumns(), 2, 5);//used the array[][]
}

If you're looking to send it to another sheet entirely, you can modify this code to do so.

I've spent some time looking into the official documentation on google app scripts, and based on that I can confirm you that there are many ways you can achieve this, one of which is explained below

getRange() + setValues()

Sample code

const startRowNumber = 1;
const starColumnNumber = 1;
const endRowNumber = dataToPut.length;
const endColumnNumber = dataToPut[0].length;

const range = sheet.getRange(startRowNumber, starColumnNumber, endRowNumber, endColumnNumber);
range.setValues(dataToPut);

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