Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update sheet data script #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions csv-to-elastic-webhook/converter/push_data_to_ES.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//To run copy the script to the sheet's appscript that needs to be ingested to ES.

function postSheetDataToElasticsearch() {
const sheetName = 'sheet1';
const elasticsearchUrl = ''; //ES url from the specific index/indices
const apiKey = ' '; //your ES apiKey
const sheetName = 'sheet1';
const elasticsearchUrl = ' ';
const apiKey = ' ';

const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const dataRange = sheet.getDataRange();
Expand All @@ -16,7 +16,13 @@ function postSheetDataToElasticsearch() {
const postData = {};
headers.forEach((header, index) => {
const value = row[index];
if (header === 'created_at') {
if (header === 'indexed_at') {
postData[header] = new Date().toISOString(); // Set the current date and time
} else if (header === 'Id') {
postData[header] = 'googlesheet-' + (rowIndex + 1); // Set the id column as "googlesheet-1", "googlesheet-2", ...
Copy link
Collaborator Author

@adamjonas adamjonas Jul 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
postData[header] = 'googlesheet-' + (rowIndex + 1); // Set the id column as "googlesheet-1", "googlesheet-2", ...
postData[header] = 'csv-upload-' + (rowIndex + 1); // Set the id column as "csv-upload-1", "csv-upload-2", ...

} else if (header === 'tags') {
postData[header] = [value.toLowerCase().trim()]; // Convert tags to lowercase
} else if (header === 'created_at') {
if (value instanceof Date && !isNaN(value)) {
postData[header] = value.toISOString();
} else if (typeof value === 'string' && value.trim() !== '') {
Expand All @@ -30,7 +36,7 @@ function postSheetDataToElasticsearch() {
postData[header] = new Date().toISOString(); // Assign current date if the value is empty or not a valid date
}
} else if (value !== '' && value !== null) {
if (header === 'authors' || header === 'categories' || header === 'tags') {
if (header === 'authors' || header === 'categories') {
postData[header] = [value.trim()];
} else {
postData[header] = value;
Expand All @@ -41,6 +47,7 @@ function postSheetDataToElasticsearch() {
const options = {
method: 'post',
contentType: 'application/json',
muteHttpExceptions: true,
headers: {
'Content-Type': 'application/json',
'Authorization': 'ApiKey ' + apiKey
Expand All @@ -50,12 +57,14 @@ function postSheetDataToElasticsearch() {

const response = UrlFetchApp.fetch(elasticsearchUrl, options);
const responseCode = response.getResponseCode();
if (responseCode === 200) {
Logger.log('Could not send data to Elasticsearch (row ' + (rowIndex + 1) + ')');
} else {
if (responseCode >= 200 && responseCode !== 400) {
Logger.log('Data sent to Elasticsearch (row ' + (rowIndex + 1) + '). Response code: ' + responseCode);
} else if (responseCode >= 400) {
Logger.log('Could not send data to Elasticsearch (row ' + (rowIndex + 1) + '). Response code: ' + responseCode);
} else {
Logger.log('Execution complete');
}

// Utilities.sleep(); // Pause execution for specified seconds
// Utilities.sleep(); // Pause execution for 2 seconds
});
}