To use this code, you need to follow these steps:
- Open a Google Sheet where you want to post updates on LinkedIn.
- Go to the “Tools” menu and select “Script editor.”
- Copy and paste the code I provided into the script editor.
- Replace the ACCESS_TOKEN placeholder in the code with your actual LinkedIn API access token.
- Save the script and close the script editor.
- Go back to your Google Sheet and enter the message and link for the update in separate columns.
- Run the script by selecting the function name (postOnLinkedIn) from the “Run” menu.
When you run the script, it will loop through the rows of data in the active sheet, retrieve the message and link for each row, and use the LinkedIn API to post the update on your behalf.
The code to paste in Apps Script section:
function postOnLinkedIn() { // Get the active sheet var sheet = SpreadsheetApp.getActiveSheet(); // Get the data range var range = sheet.getDataRange(); var values = range.getValues(); // Loop through the rows of data for (var i = 1; i < values.length; i++) { var row = values[i]; // Get the message and link from the row var message = row[0]; var link = row[1]; // Post on LinkedIn var options = { "method": "post", "payload": { "comment": message, "content": { "contentEntities": [ { "thumbnails": [ { "resolvedUrl": "https://example.com/image.jpg" } ], "title": "Title", "description": "Description", "contentUrl": link, "entityLocation": link } ], "title": "Title", "description": "Description" }, "visibility": { "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC" } }, "headers": { "Authorization": "Bearer ACCESS_TOKEN", "Content-Type": "application/json" } }; var response = UrlFetchApp.fetch("https://api.linkedin.com/v2/ugcPosts", options); Logger.log(response.getContentText()); }
}