最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Try construct dynamic cell reference from a google sheet in google app script for CopyTo method - Stack Overflow

programmeradmin0浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

Using 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

  1. Function MySpecificForm() and Function addRecord(WhoIs) I corrected the proper casing for this.

  2. 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.

  3. 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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论