Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

google apps script - place variable value into a specific cell

Hi just a quick easy one, im not very experienced with scripts in sheets. I have my variables set up but i would just like to return the variable to a specific cell when the script is ran. I marked as a comment in the script below what i would like from it. Thanks!

// Menu for testing script
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Record value')
      .addItem('Record now','tester')
      .addToUi();
}

function tester() {
   var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Matt");
   var value1 = SpreadsheetApp.getActiveSheet().getRange('L3').getValue();
    // i would like to append "value1" to to cell "B3" here
}
question from:https://stackoverflow.com/questions/65889838/place-variable-value-into-a-specific-cell

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use setValue(value) to append a value into a cell, keeping the original value intact:

If the destination sheet is the active sheet:

var dest = SpreadsheetApp.getActiveSheet().getRange("B3");
var original = dest.getValue();
dest.setValue(original+value1);

If the destination sheet is the named one:

var dest = sheet.getRange("B3");
var original = dest.getValue();
dest.setValue(original+value1);

References:

setValue(value)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...