+# Find the newest message, and what it is linked to.
+# TODO: Support more than one, and test better for errors here.
+my $q = $dbh->prepare('select * from message_sheet_link where channel=? order by ts desc limit 1');
+$q->execute($config::invitation_channel);
+my $linkref = $q->fetchrow_hashref;
+my $invitation_ts = $linkref->{'ts'};
+my $wanted_sheet_title = $linkref->{'sheet_title'};
+die "Could not get newest sheet title" if (!defined($wanted_sheet_title));
+
+# See if we have any spreadsheets that match this title.
+my $response = $ua->get('https://sheets.googleapis.com/v4/spreadsheets/' . $config::sheet_id . '?key=' . $config::gsheets_api_key . '&fields=sheets/properties',
+ Authorization => 'Bearer ' . $token
+);
+my $sheets_json = JSON::XS::decode_json($response->decoded_content);
+my ($tab_name, $tab_id);
+for my $sheet (@{$sheets_json->{'sheets'}}) {
+ my $title = $sheet->{'properties'}{'title'};
+ my $sheet_id = $sheet->{'properties'}{'sheetId'};
+ if ($title =~ /\Q$wanted_sheet_title\E/) {
+ skv_log("Synkroniserer ($config::invitation_channel, $invitation_ts) mot arket “$title” (fane-ID $sheet_id).");
+ $tab_name = $title;
+ $tab_id = $sheet_id;
+ last;
+ }
+}
+if (!defined($tab_name)) {
+ skv_log("Fant ikke noen fane med “$wanted_sheet_title” i navnet; kan ikke synkronisere.\n");
+ sheet_batch_update($ua, $token, [ serialize_skv_log_to_sheet() ]);
+ die;
+}
+