简体   繁体   中英

How to take value from cell in Google sheets, to be used as number variable in Google Script

I am trying to use the value of Cell A3 as a variable to specify the exact size of a particular array. Code below isn't working, why?

A3 is a =COUNTA(B2:VF2)-1 which currently evaluates to 446

function RangeCopyToLog() {
  var spreadsheet = SpreadsheetApp.getActive();
  var destSheet = spreadsheet.getSheetByName("Xp Log");
  var lastRow = destSheet.getLastRow();
  var copyColEnd = destSheet.getRange('A3') + 1 //Problem is here
  destSheet.insertRows(lastRow + 1,1);
  destSheet.getRange(1,3,1,copyColEnd).copyTo(destSheet.getRange(34,3,1,copyColEnd), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
 // destSheet.getRange('D1:D30').copyTo(destSheet.getRange(lastRow + 1,1), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, true);
 // destSheet.getRange('D1:D30').clearContent();
};

Error presented is: "Cannot convert Range1 to (class)."

.getRange() only returns the range, not the value. Change this line

var copyColEnd = destSheet.getRange('A3') + 1 

to

var copyColEnd = destSheet.getRange('A3').getValue() + 1 

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