H
H
Hattori_Hanzo2020-12-02 10:56:03
Google Apps Script
Hattori_Hanzo, 2020-12-02 10:56:03

How to ignore the creation of a document in the absence of a date?

There is a code that should create a Google document from the data in the Google spreadsheet. If the document is already created, it shouldn't be re-created, but the problem is that I have a table already with numbers, and the code starts creating an extra document, then throws an error and stops execution. How can I make sure that the code does not create duplicate documents and does not create an extra document if the date is missing?

function createDocument() {
  var value = Sheets.Spreadsheets.Values.get('1s4rk03q4meZ2Ehr_MZyxFLfQr2XjJj9QWeOV97iMAjg', 'A2:R');
  var tamplate = '1Db4Zlm6Q0zCLQx62WLuqi63K4vPasWVXLfjJwsIdyl4';
  for (var i = 0; i <value.values.length; i ++){
  var number = value.values[i][0]; // Номер заявки
  var date = value.values[i][2]; //Дата
  var applicant = value.values[i][4]; // Заявитель
    var description = value.values[i][6]; //Описание
    var place = value.values[i][5]; //Место
    var executor = value.values[i][14]; //Исполнитель
    var complete = value.values[i][13]; //Выполненные работы
    console.log(date)
    var files = DriveApp.getFilesByName(('Заявка № ' + number));
    if (files.hasNext() === true){
    }
    else{
  var documentId = DriveApp.getFileById(tamplate).makeCopy().getId();
  DriveApp.getFileById(documentId).setName('Заявка № ' + number);
    var body = DocumentApp.openById (documentId) .getBody ();
    body.replaceText('##Заявка №##', number);
    body.replaceText('##Дата##', date);
    body.replaceText('##Заявитель##', applicant);
    body.replaceText('##Описание##', description);
    body.replaceText('##Место##', place);
    body.replaceText('##Исполнитель##', executor);
    body.replaceText('##Выполненная работа##', complete);
    }
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Hattori_Hanzo, 2020-12-03
@Hattori_Hanzo

if (files.hasNext() === true){
}
else if(date !== undefined){

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question