K
K
KriptoAnalytic2018-02-06 14:11:31
Google Apps Script
KriptoAnalytic, 2018-02-06 14:11:31

How to shorten this google apps script code?

Hello,
There is a table with a list of all available tables (the names of these tables may change). To keep the list of titles up-to-date, I wrote a script that takes the id of the tables from the "C" column, opens the table by this id, takes the title and inserts it into the "B" column. The problem is that the list is constantly updated and it is not convenient to change the script, so I would like to write it as a loop

function GetSpreadSheetsName33() {
  var SpreadSheets = SpreadsheetApp.openById('Тут ID Таблицы').getSheetByName('Accesses');
  var Data = SpreadSheets.getRange("C2").getValue();
  var SheetName = SpreadsheetApp.openById(Data).getName();
  SpreadSheets.getRange("B2").setValue(SheetName);
   
  var Data = SpreadSheets.getRange("C3").getValue();
  var SheetName = SpreadsheetApp.openById(Data).getName();
  SpreadSheets.getRange("B3").setValue(SheetName);
  
  var Data = SpreadSheets.getRange("C4").getValue();
  var SheetName = SpreadsheetApp.openById(Data).getName();
  SpreadSheets.getRange("B4").setValue(SheetName); 
  
  var Data = SpreadSheets.getRange("C5").getValue();
  var SheetName = SpreadsheetApp.openById(Data).getName();
  SpreadSheets.getRange("B5").setValue(SheetName);    
  
  var Data = SpreadSheets.getRange("C6").getValue();
  var SheetName = SpreadsheetApp.openById(Data).getName();
  SpreadSheets.getRange("B6").setValue(SheetName);  
  
  var Data = SpreadSheets.getRange("C7").getValue();
  var SheetName = SpreadsheetApp.openById(Data).getName();
  SpreadSheets.getRange("B7").setValue(SheetName);  
  }

By the "poke" method it turned out, only it does not work
function GetSpreadSheetsName() {
var i = 2;
var SpreadSheets = SpreadsheetApp.openById('Тут ID таблицы').getSheetByName('Accesses');
  while (SpreadSheets.getRange("C"+i).getValue().lenght>1)
  {
  i++;
    var Data = SpreadSheets.getRange("С"+i).getValue();
  var SheetName = SpreadsheetApp.openById(Data).getName();
  SpreadSheets.getRange("B"+i).setValue(SheetName);
  }
    }

This question on StackOverFlow

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KriptoAnalytic, 2018-02-06
@KriptoAnalytic

https://ru.stackoverflow.com/a/781142/284146

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question