Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @fileoverview Google Apps Script to import new rows from a source sheet to a destination sheet based on unique timestamps.
- *
- * Author: u/IAmMoonie
- * @see https://www.reddit.com/r/GoogleAppsScript/comments/1fi5vw5/compare_timestamps_on_both_sheets_only_copy/
- * Version: 1.0
- */
- /**
- * Configuration object for the importNewRequests function.
- *
- * @typedef {Object} Config
- * @property {string} sourceID - The ID of the source Google Sheets spreadsheet.
- * @property {string} formRange - The range in the source sheet to check for new rows, formatted as 'SheetName!A1:R'.
- * @property {string} workingRangeStart - The starting cell in the destination sheet where new rows will be appended.
- * @property {string} timestampColumn - The letter of the column in the source sheet that contains the timestamps.
- */
- const config = {
- sourceID: "1MTvMNwNYlKqfgaU_lvv-RqW4Jofs2Z1rrlLRAj4yFzM",
- formRange: "Master!A1:R",
- workingRangeStart: "Working!A1",
- timestampColumn: "A"
- };
- /**
- * WARNING: Do not edit anything below this point unless you are familiar with Google Apps Script and the purpose of the code.
- */
- /**
- * Imports new requests from the source sheet to the destination sheet if they have unique timestamps.
- */
- const importNewRequests = () => {
- const sourceSpreadsheet = SpreadsheetApp.openById(config.sourceID);
- const sourceSheet = sourceSpreadsheet.getSheetByName(
- config.formRange.split("!")[0]
- );
- const destSheet = sourceSpreadsheet.getSheetByName(
- config.workingRangeStart.split("!")[0]
- );
- const timestampColIndex = getColumnIndex_(config.timestampColumn);
- const sourceValues = sourceSheet.getRange(config.formRange).getValues();
- const sourceRowCount = sourceValues.length;
- console.info(`Source sheet contains ${sourceRowCount} row(s).`);
- const lastDestRow = getLastNonEmptyRow_(destSheet, timestampColIndex + 1);
- const destRowCount = lastDestRow;
- console.info(`Destination sheet currently has ${destRowCount} row(s).`);
- const destTimestamps = new Set(
- destSheet
- .getRange(1, timestampColIndex + 1, lastDestRow, 1)
- .getValues()
- .flat()
- .map((ts) => new Date(ts).getTime())
- );
- const newRows = [];
- console.info(
- "Checking rows in the source sheet that have a different timestamp compared to the destination sheet"
- );
- sourceValues.forEach((row, index) => {
- const timestamp = new Date(row[timestampColIndex]).getTime();
- console.info(`Checking row ${index + 1}: Timestamp: ${timestamp}`);
- if (timestamp && !destTimestamps.has(timestamp) && !isRowEmpty_(row)) {
- console.info(
- `New row detected with timestamp ${new Date(
- timestamp
- )}, adding to newRows...`
- );
- newRows.push(row);
- } else {
- console.info(
- `Row ${
- index + 1
- } already exists in Working sheet or missing timestamp, skipping.`
- );
- }
- });
- const newRowCount = newRows.length;
- console.info(`${newRowCount} new row(s) meet the requirements.`);
- if (newRowCount > 0) {
- const destRange = destSheet.getRange(
- lastDestRow + 1,
- 1,
- newRowCount,
- newRows[0].length
- );
- console.info(`Copying ${newRowCount} new row(s) to the destination sheet.`);
- destRange.setValues(newRows);
- } else {
- console.info("No new rows to copy.");
- }
- };
- /**
- * Gets the last non-empty row in a specific column of a sheet.
- *
- * @param {Sheet} sheet - The sheet to check.
- * @param {number} column - The column number to check for non-empty rows.
- * @return {number} The index of the last non-empty row.
- */
- const getLastNonEmptyRow_ = (sheet, column) => {
- const data = sheet.getRange(1, column, sheet.getLastRow()).getValues();
- for (let i = data.length - 1; i >= 0; i--) {
- if (data[i][0] !== "") {
- return i + 1;
- }
- }
- return 0;
- };
- /**
- * Checks if a row is empty.
- *
- * @param {Array} row - The row to check.
- * @return {boolean} True if the row is empty, false otherwise.
- */
- const isRowEmpty_ = (row) => row.every((cell) => cell === "");
- /**
- * Gets the column index from a letter.
- *
- * @param {string} columnLetter - The column letter (e.g., 'A').
- * @return {number} The index of the column (0-based).
- */
- const getColumnIndex_ = (columnLetter) =>
- columnLetter.toUpperCase().charCodeAt(0) - 65;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement