H
H
Hattori_Hanzo2020-11-11 09:30:10
Google Apps Script
Hattori_Hanzo, 2020-11-11 09:30:10

How to create only 1 document when transferring data from google sheets?

Good afternoon! I wrote a code that creates from tables and replaces certain data in the document template, but I have a question, how to make sure that documents are not re-created? Because now when you run the code, it re-creates the documents every time.

function createDocument() {
  var headers = Sheets.Spreadsheets.Values.get('1WRDbmvKjwulxjZjuH33u18d1IMQnq68EYugs2YDgxqw', 'A1:G1');
  var value = Sheets.Spreadsheets.Values.get('1WRDbmvKjwulxjZjuH33u18d1IMQnq68EYugs2YDgxqw', 'A2:G');
  var tamplate = '1H4VgpPqksqA2RjoGbrDcKttZxZvFbJqbbMhIKwA9CgQ';
  for (var i = 0; i <value.values.length; i ++){
  var number = value.values[i][1];
  var date = value.values[i][0];
  var applicant = value.values[i][2];
    var description = value.values[i][3];
    var place = value.values[i][4];
    var executor = value.values[i][6];
    
  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);
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Stoyanov, 2020-11-11
@stomaks

As an option, add a status column in the original Google spreadsheet.
When re-running the script, ignore the lines in which the status is "ready" for example.
Or save the id or link to the document.
Another way (worse, but in some cases it also happens) is to put a label (developer metadata) into the document, and before creating a new document, check whether there is such a document on disk.
The third way, delete a row from the Google spreadsheet or move it to the adjacent "Processed" sheet.
The fourth way, show a modal window in which you ask the user what range of rows in the Google spreadsheet to process.
... and a bunch of options)
---
Maxim Stoyanov (stomaks), developer of Google Apps Script.
g-apps-script.com
stomaks.me

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question