简体   繁体   中英

How can I use a script in google sheets to copy an image from one cell to another?

I have a script I am using to copy data from a form in a Google Sheet. ( Yes I know I can just use Google Forms but the client I am working with would like to keep it all in one Google Sheet) Here is the code I am using now, and it works great. It does exactly what I want, but there is one field that an image is to be placed into. I have it set up so the user goes to "Insert" and then from there inserts an image into the cell. I can manually copy and paste the image from one sheet to another but when I run the script it will not copy over the image all the other data does copy over. Any help would be amazing. Here is the script. The image is in cell I3, as I said everything else works as expected.

function submitData()  {
var ss        = SpreadsheetApp.getActiveSpreadsheet();
var formSS    = ss.getSheetByName("Form"); //Form Sheet
var datasheet = ss.getSheetByName("Data"); //Data Sheet

 //Input Values
 var values = [[formSS.getRange("E3").getValue(),
             formSS.getRange("I3").getValue(),
             formSS.getRange("E6").getValue(),
             formSS.getRange("I6").getValue(),
             formSS.getRange("E10").getValue(),
             formSS.getRange("I10").getValue(),
             formSS.getRange("E15").getValue(),
             formSS.getRange("I15").getValue(),
             formSS.getRange("E19").getValue(),
             formSS.getRange("I19").getValue(),
             formSS.getRange("E22").getValue(),
             formSS.getRange("I22").getValue(),
             formSS.getRange("E25").getValue(),
             formSS.getRange("I25").getValue(),
             formSS.getRange("E28").getValue()]];

datasheet.getRange(datasheet.getLastRow()+1, 1, 1, 15).setValues(values);
formSS.getRange('E3:E29').clearContent();
formSS.getRange('I3:I29').clearContent();


} 

I have modified your script to fit the image in the Data sheet.

IMPORTANT: Unmerge the picture cells on the Form sheet and leave the background white. What the script will do is move the values from the Form to the Data (the background color will be moved as well)

The script will get the first value alone and put it in the first column, last row +1.

Now the last row is the one required (not the last +1), and then the script moves the image and inserts the other values as you were doing before.

The script:

function submitData() {
  var ss        = SpreadsheetApp.getActiveSpreadsheet();
  var formSS    = ss.getSheetByName("Form"); //Form Sheet
  var datasheet = ss.getSheetByName("Data"); //Data Sheet

  //Input Values
  var values = [[formSS.getRange("E6").getValue(),
                 formSS.getRange("I6").getValue(),
                 formSS.getRange("E10").getValue(),
                 formSS.getRange("I10").getValue(),
                 formSS.getRange("E15").getValue(),
                 formSS.getRange("I15").getValue(),
                 formSS.getRange("E19").getValue(),
                 formSS.getRange("I19").getValue(),
                 formSS.getRange("E22").getValue(),
                 formSS.getRange("I22").getValue(),
                 formSS.getRange("E25").getValue(),
                 formSS.getRange("I25").getValue(),
                 formSS.getRange("E28").getValue()]];

  // Get the first value and insert into the first column
  var firstColumn = formSS.getRange("E3").getValue()
  datasheet.getRange(datasheet.getLastRow()+1, 1,).setValue(firstColumn);

  // Get the image and intert into the second column of the same row
  var image = formSS.getRange("I3").moveTo(datasheet.getRange(datasheet.getLastRow(), 2));

  // Get all the other values and insert them into the other columns of the same row
  datasheet.getRange(datasheet.getLastRow(), 3, 1, 13).setValues(values);
  formSS.getRange('E3:E29').clearContent();
  formSS.getRange('I3:I29').clearContent();

}

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