Search code examples
google-sheetsgoogle-apps-script

Function does not run when calling `SpreadsheetApp.getActiveSpreadsheet().getRange();`


I want to get the strings in a range Range(12,3,1,4), then strip the last characters of the strings, put the last characters into an array sol[], the rest I put in another array abcd[]. I then put the values ​​of these arrays into the ranges Range(1,3,1,4) and Range(2,3,1,4). I wrote the following code to try:

function getStringDSabcs(){
  var sol = [];
  var abcd = [];
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName("tests");

  const numbers2D = sh.getRange(12,3,1,4).getValues();

  const numbers1D = numbers2D.reduce((acc, row) => {
  return acc.concat(row);
}, []);
console.log(numbers1D); // [1, 2, 3, 4, 5, 6, 7, 8, 9]

numbers1D.forEach(function(number) {
  console.log(number); // In ra từng số trong mảng
  var lastChar = number.charAt(number.length - 1); 
  var firstPart = number.substring(0, number.length - 1); 
  console.log("Phần đầu mỗi chuỗi là: "+firstPart+" và phần cuối là: "+lastChar);
  sol.push(lastChar);
  abcd.push(firstPart);
  console.log(sol); 
  console.log(abcd);

});
}

This function works very well. Now I add the rest

sh.getRange(1,3,1,4).setValues([abcd]);
sh.getRange(2,3,1,4).setValues([sol]);

Tức là mã của tôi là

function getStringDSabcs(){
  var sol = [];
  var abcd = [];
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName("tests");

  const numbers2D = sh.getRange(12,3,1,4).getValues();

  const numbers1D = numbers2D.reduce((acc, row) => {
  return acc.concat(row);
}, []);
console.log(numbers1D); // [1, 2, 3, 4, 5, 6, 7, 8, 9]

numbers1D.forEach(function(number) {
  console.log(number); // In ra từng số trong mảng
  var lastChar = number.charAt(number.length - 1); 
  var firstPart = number.substring(0, number.length - 1); 
  console.log("Phần đầu mỗi chuỗi là: "+firstPart+" và phần cuối là: "+lastChar);
  sol.push(lastChar);
  abcd.push(firstPart);
  console.log(sol); 
  console.log(abcd);
sh.getRange(1,3,1,4).setValues([abcd]);
sh.getRange(2,3,1,4).setValues([sol]);

});
}

This new function doesn't work: The execution log still says it started and finished, but the start and end are only 1 second apart. Also console.log and return values ​​are not present. I have checked the code very carefully but don't know where the error occurred.

I saw somewhere an article about Function does not run when calling SpreadsheetApp.getActiveSpreadsheet().getRange();.


Solution

  • try:

    function getStringDSabcs(){
      var sol = [];
      var abcd = [];
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sh = ss.getSheetByName("tests");
    
      const numbers2D = sh.getRange(12, 3, 1, 4).getValues();
      const numbers1D = numbers2D[0];  // Since you are getting a 1x4 range, numbers1D is just the first row
    
      numbers1D.forEach(function(number) {
        console.log(number); // Log each number in the array
        var lastChar = number.charAt(number.length - 1); 
        var firstPart = number.substring(0, number.length - 1); 
        console.log("Phần đầu mỗi chuỗi là: " + firstPart + " và phần cuối là: " + lastChar);
        sol.push(lastChar);
        abcd.push(firstPart);
      });
    
      console.log("Final sol array:", sol); 
      console.log("Final abcd array:", abcd);
    
      sh.getRange(1, 3, 1, 4).setValues([abcd]);
      sh.getRange(2, 3, 1, 4).setValues([sol]);
    }