I try to construct code as follow to make google app script being dynamic to copy image that is stored in cell from on any UserForm sheet which call this function to a log data sheet. The images are located in range D16:D18 in the UserForm. I have more than one identical userForm sheets to allow several user input their data to their UserForm then it will be saved to the same log sheet.
Function MySpecificForm(){
var User1 = "User 1 Form"
addRecord(User1)
//this is one example that calling the add record procedure
}
Function addRecord(WhoIs){
var SourceSheetRange = "'" + "\\" + "'" + WhoIs + "'" + "\\" + "'" + "!D16:D18'"
console.log (SourceSheetRange)
spreadsheet.getRange(SourceSheetRange).copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, true);
spreadsheet.getRange('O7').activate();
}
When I run the code I get error as follow
Console Log result
Though the result already the same as original syntax I get from record macro for CopyTo method. See highlighted in the image below
Try Mimic Highlighted part
Hence, it is not working in my code. Is there any lack in my reconstructed string?
This is for google spreadsheet.
I try to construct code as follow to make google app script being dynamic to copy image that is stored in cell from on any UserForm sheet which call this function to a log data sheet. The images are located in range D16:D18 in the UserForm. I have more than one identical userForm sheets to allow several user input their data to their UserForm then it will be saved to the same log sheet.
Function MySpecificForm(){
var User1 = "User 1 Form"
addRecord(User1)
//this is one example that calling the add record procedure
}
Function addRecord(WhoIs){
var SourceSheetRange = "'" + "\\" + "'" + WhoIs + "'" + "\\" + "'" + "!D16:D18'"
console.log (SourceSheetRange)
spreadsheet.getRange(SourceSheetRange).copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, true);
spreadsheet.getRange('O7').activate();
}
When I run the code I get error as follow
Console Log result
Though the result already the same as original syntax I get from record macro for CopyTo method. See highlighted in the image below
Try Mimic Highlighted part
Hence, it is not working in my code. Is there any lack in my reconstructed string?
This is for google spreadsheet.
Share Improve this question edited Feb 3 at 12:57 anna liem asked Feb 3 at 9:26 anna liemanna liem 115 bronze badges 2- 1. Yes, it is the sheet name. See the second picture I attached. However, the sheet source name may differ if this function is called from other sheet. That's why I use variable. 2. Please read my code carefully before you respond. It is stated there. 3. Do not mind with the rest of the line. Be focus with the one that has CopyTo method. If that line works, the rest will follow. – anna liem Commented Feb 3 at 12:54
- Please include your answer as text rather than screenshots. Text is easier to read, search, and reference, and it helps avoid unnecessary clicks. For more guidance see How do I write a good answer. – Borewit Commented Feb 5 at 16:32
1 Answer
Reset to default 1Using CopyTo to pass on Data
With some intriguing events there is no documentation that supports that it supposed to have an error you can raise an Issue with this so you can learn more about this situation, it seems that your issue is with how the range is being called, with multiple tests I figured out that it stops working if you put the code your way. I put up a way that manages to execute the copyTo() by making some changes on the ranges.
Changes I made with the code
Function MySpecificForm()
andFunction addRecord(WhoIs)
I corrected the proper casing for this.Your screenshots show that your Sheet Name is
'/'User 1 Form /'
, however there are different variations of this throughout the code. You should really look into that.Your whole range creation part of your code.
Recommended Solution:
function addRecord(whoIs) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("'"+"/"+"'" +whoIs + "/"+"'");
var range = sheet.getRange("D16:D18");
var dest = ss.getActiveSheet().getRange("O7").activate();
range.copyTo(
ss.getActiveSheet().getActiveCell(),
SpreadsheetApp.CopyPasteType.PASTE_VALUES,
true);
}
function mySpecificForm(){
var user1 = "User 1 Form";
addRecord(user1);
}
References:
copyTo