Я использую getValue и получаю лист ошибок. DataAsArr.getValue не является функцией - PullRequest
0 голосов
/ 26 мая 2020

Я пытаюсь проверить столбец строки, если флажок установлен и появляется ошибка, я включил часть сценария под строкой

             if  ( sheetDataAsArr.getValue(i+2, U) == true ) {    

Вот код ниже, у меня есть использовать getRange и getValue (i + 2, U) == true) посоветуйте, пожалуйста?

Ошибка Sheet.DataAsArr.getValue не является функцией

    // lets get all the data in the active Sheet (or whichever Sheet)
    const sheetDataAsArr = SpreadsheetApp.getActiveSheet().getDataRange().getValues(); // 2020Members 


    // Start at first row 
    var i = 0;

    // Mark the rows as apporved  
     sheetDataAsArr.forEach(function(row,i)     {   
     // The i is a index 
     // 

     var V = 22;   // V defined here
     var U = 21;   // U defined here


       if  ( sheetDataAsArr.getValue(i+2, U) == true ) {    


          // method getRange(row, column, optNumRows, optNumColumns)
          ws.getRange(i+2, V).setValue(appoved); // Note: argument 3, 4, is optional actual worksheet 
          // The i+2 is because the data starts on the second row first row are headers    
          // return; 
          Logger.log('INDEX i is :' + i);
          Logger.log('ROW row is :' + row[0]);  // The email address 

          i++; // Must auto increment i  

        } 
        i++; // Must auto increment i  

      } 

     } ) ; 

Ответы [ 3 ]

0 голосов
/ 27 мая 2020

Я предполагаю здесь довольно много, но я думаю, что это то, для чего вы снимали:

function getAllData() {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getActiveSheet();
  const sr=2;
  const rg=sh.getRange(sr,21,sh.getLastRow()-sr+1,1);
  const org=sh.getRange(sr,22,sh.getLastRow()-sr+1,1);
  const ovs=org.getValues();
  const vs=rg.getValues();
  vs.forEach(function(r,i){if(r[0]==true){ovs[i][0]='Approved';}});
  org.setValues(ovs);
}
0 голосов
/ 27 мая 2020

Решение

Это быстрая и простая реализация того, к чему вы стремились абстрагироваться, так что любой может легко адаптировать его к своей конкретной c ситуации.

Проверьте значения диапазона флажков, и, если они отмечены, установите значения указанного c диапазона.

Этот фрагмент кода использует функцию onEdit(), который будет изменять значения всякий раз, когда пользователь в электронной таблице устанавливает флажок. Если вы не хотите, чтобы этот триггер был активен, просто измените имя функции. В этом случае я проверяю диапазон флажков от A1:A100 и устанавливаю значения B1:B100, если флажок в столбце A отмечен. Код имеет понятные комментарии:

function onEdit() {
  // Get sheet by name
  var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
  
  // Get our range where we have the checkboxes. GetValues returns an array of arrays
  // for rows and columns, if we flatten it using the flat function we will end up with the
  // desired 1D array which lets us avoid usingthe forEach function (and wrting less code ! :D)
  var checkboxRangeValues = sheet.getRange('A1:A100').getValues().flat();
  
  // for each cell in the check box range
  for(i=0;i<checkboxRangeValues.length;i++){
  
  // check if that specific cell is marked
    if(checkboxRangeValues[i]==true){
    // set the value of your desired cell to approved
    // (note this must be a string if it is a word), in this case we are setting to approved
    // the cells in the column B (2), i+1 is because our indexes start by 0 while our row
    // numbers actually start by 1
      sheet.getRange(i+1, 2).setValue('approved');
    }
  }
}

Надеюсь, это вам помогло. Дайте мне знать, если вам еще что-нибудь понадобится или вы чего-то не поняли. :)

0 голосов
/ 27 мая 2020

Ваш код передает два аргумента в getValue, но у него не должно быть никаких аргументов.

Ссылка

https://developers.google.com/apps-script/reference/spreadsheet/range#getvalue

...