title: How do I Set a formula to value in Google scripts?
body: <p>I have written a code that generates random numbers and concatenates them with text. I am using this function to fill a range of cells (from A1 to Z10):</p>
  
  <pre><code>function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var cell = sheet.getRange("A1:Z10");
  cell.setFormula('=CONCATENATE("61", RANDBETWEEN(1000000000, 9999999999), "@text")');
  }
  </code></pre>
  
  <p>The problem with this is that every time I reload the page, the numbers are recalculated, there is no static value. I want the values in my range to become static, so that every time I reload the page, the numbers in between the "61" and "@text" remain the same. I have tried the following two codes, and neither has worked. The first did not receive an error message, but simply did not keep the values static, while the second received the error message ""</p>
  
  <pre><code>function copyFormulasToValues() { var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var sourceSheet = ss.getSheetByName("Sheet1"); 
  var destinationSheet = ss.getSheetByName("Sheet1"); 
  var range = sourceSheet.getRange(1,1,1,1);
  range
    .copyValuesToRange(destinationSheet, 1, 1, range.getNumRows(), range.getNumColumns());
   };
  </code></pre>
  
  <p>The second code:</p>
  
  <pre><code>var studentSheet = SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheet = SpreadsheetApp.getActiveSpreadsheet();
  
  // copyTo rows
  sourceSheet.getRange(1, 1, 10, 26).copyTo(studentSheet.getRange(1, 1, 10, 26), {contentsOnly: 
  true});
  
  SpreadsheetApp.flush(); // applies all values to the student spreadsheet
                      // and therefore all the formula cells update
  // get value from calculated formula cell
  studentSheet.getRange(1, 1, 10, 26).copyTo(sourceSheet.getRange(1, 1, 10, 26), {contentsOnly: 
  true});
  }
  </code></pre>
  
  <p>The error message:</p>
  
  <blockquote>
    <p>Cannot find method getRange(number,number,number,number). (line 11, file "Code")</p>
  </blockquote>
  
  <p>I just want to copy and paste the values in the same spreadsheet so they remain static. How can I achieve this?</p>
  
  <p><strong>NOTE 1.</strong></p>
  
  <p>I really appreciate the help Tanaike. I tried out the script (which I have posted below), but when I try and run it, the function runs the code, and then just entirely deletes the values.</p>
  
  <pre><code>function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var cell = sheet.getRange("A1:Z10");
  cell.setFormula('=CONCATENATE("61", RANDBETWEEN(1000000000, 9999999999), 
  "@text.com")');
  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Sheet1");
  var range = sheet.getRange("A1:Z10");
  range.copyValuesToRange(sheet, 1, range.getNumColumns(), 1, 
  range.getNumRows());
  }
  </code></pre>
  
  <p>Perhaps I am doing something wrong?</p>