/** * @OnlyCurrentDoc * @see https://developers.google.com/apps-script/guides/services/authorization * * @requires https://www.googleapis.com/auth/script.external_request */ function getJWTToken() { const url = 'https://time4realestate.com/wp-json/jwt-auth/v1/token'; const payload = { username: 'kpstg', password: 'TLV%d7&GS3^cVW43^u(Ny!#&' }; const options = { method: 'POST', contentType: 'application/json', payload: JSON.stringify(payload), muteHttpExceptions: true }; const response = UrlFetchApp.fetch(url, options); const result = JSON.parse(response.getContentText()); if (response.getResponseCode() === 200 && result.token) { return result.token; } else { throw new Error('Failed to get JWT token: ' + result.message); } } function createWordPressProperty(property) { const token = getJWTToken(); const options = { method: 'POST', contentType: 'application/json', headers: { 'Authorization': 'Bearer ' + token }, payload: JSON.stringify({ title: property.title, price: property.price, address: property.address, description: property.description, bedrooms: property.bedrooms, bathrooms: property.bathrooms, size: property.size, type: property.type, image_url: property.image_url }), muteHttpExceptions: true }; const url = 'https://time4realestate.com/wp-json/realestate/v1/properties'; const response = UrlFetchApp.fetch(url, options); const result = JSON.parse(response.getContentText()); if (response.getResponseCode() !== 200 && response.getResponseCode() !== 201) { throw new Error('Failed to create property: ' + result.message); } } function updateWordPressProperty(property) { const token = getJWTToken(); const options = { method: 'POST', contentType: 'application/json', headers: { 'Authorization': 'Bearer ' + token }, payload: JSON.stringify({ title: property.title, price: property.price, address: property.address, description: property.description, bedrooms: property.bedrooms, bathrooms: property.bathrooms, size: property.size, type: property.type, image_url: property.image_url }), muteHttpExceptions: true }; const url = 'https://time4realestate.com/wp-json/realestate/v1/properties/' + property.ID; const response = UrlFetchApp.fetch(url, options); const result = JSON.parse(response.getContentText()); if (response.getResponseCode() !== 200 && response.getResponseCode() !== 201) { throw new Error('Failed to update property: ' + result.message); } } function onEdit(e) { if (e && e.range) { const editedRow = e.range.getRow(); const sheet = e.source.getActiveSheet(); const dataRange = sheet.getDataRange(); const values = dataRange.getValues(); if (editedRow > 1 && editedRow <= values.length) { const property = { ID: values[editedRow - 1][0] !== '' ? values[editedRow - 1][0] : null, title: values[editedRow - 1][1], price: values[editedRow - 1][2] !== '' ? values[editedRow - 1][2] : null, address: values[editedRow - 1][3] !== '' ? values[editedRow - 1][3] : null, description: values[editedRow - 1][4] !== '' ? values[editedRow - 1][4] : null, bedrooms: values[editedRow - 1][5] !== '' ? values[editedRow - 1][5] : null, bathrooms: values[editedRow - 1][6] !== '' ? values[editedRow - 1][6] : null, size: values[editedRow - 1][7] !== '' ? values[editedRow - 1][7] : null, type: values[editedRow - 1][8] !== '' ? values[editedRow - 1][8] : null, image_url: values[editedRow - 1][9] !== '' ? values[editedRow - 1][9] : null }; if (!property.ID) { try { createWordPressProperty(property); Logger.log('New property created successfully.'); } catch (error) { Logger.log('Failed to create new property: ' + error); } } else { try { updateWordPressProperty(property); Logger.log('Property updated successfully.'); } catch (error) { Logger.log('Failed to update property: ' + error); } } } } else { console.log("Event object or range is undefined."); } } const WORDPRESS_API_URL = 'https://time4realestate.com/wp-json/realestate/v1/properties'; function fetchPropertiesFromWordPress() { const response = UrlFetchApp.fetch(WORDPRESS_API_URL); const properties = JSON.parse(response.getContentText()); const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); sheet.clear(); const headers = ['ID', 'Title', 'Price', 'Address', 'Description', 'Bedrooms', 'Bathrooms', 'Size', 'Type', 'Image URL']; sheet.appendRow(headers); properties.forEach(property => { sheet.appendRow([ property.ID, property.title, property.price, property.address, property.description, property.bedrooms, property.bathrooms, property.size, property.type, property.image_url ]); }); } function onOpen() { const ui = SpreadsheetApp.getUi(); ui.createMenu('WordPress') .addItem('Fetch Properties', 'fetchPropertiesFromWordPress') .addToUi(); } {"version":"1.0","provider_name":"Time 4 Real Estate","provider_url":"https:\/\/time4realestate.com","author_name":"kpstg","author_url":"https:\/\/time4realestate.com\/author\/kpstg\/","title":"New Pre-construction Sales","type":"rich","width":600,"height":338,"html":"
New Pre-construction Sales<\/a><\/blockquote>